tar命令的第一个参数必须是一个函数名(或者简写),而且只能有一个函数,常见语法如下:
NAME
tar -- The GNU version of the tar archiving utility
SYNOPSIS
tar [-] A --catenate --concatenate | c --create | d --diff --compare | --delete | r --append | t --list | --test-label | u
--update | x --extract --get [options] [pathname ...]
FUNCTION LETTERS
Main operation mode:
-A, --catenate, --concatenate
append tar files to an archive
-c, --create
create a new archive
-d, --diff, --compare
find differences between archive and file system
--delete
delete from the archive (not on mag tapes!)
-r, --append
append files to the end of an archive
-t, --list
list the contents of an archive
--test-label
test the archive volume label and exit
-u, --update
only append files newer than copy in archive
-x, --extract, --get
extract files from an archive
OTHER OPTIONS
Operation modifiers:
-f, --file ARCHIVE
use archive file or device ARCHIVE
-k, --keep-old-files
don't replace existing files when extracting,
-v, --verbose
verbosely list files processed
-w, --interactive, --confirmation
ask for confirmation for every action
-z, --gzip, --gunzip --ungzip
-Z, --compress, --uncompress
EXAMPLES
Create archive.tar from files foo and bar.
tar -cf archive.tar foo bar
List all files in archive.tar verbosely.
tar -tvf archive.tar
Extract all files from archive.tar.
tar -xf archive.tar
示例1,将文件 mongodb.rpm MongoDBIntro.pdf 打包成一个文件 mongo.tar
root@db2a:/tmp# ls Mon* mon*
MongoDBIntro.pdf mongodb.rpm
root@db2a:/tmp# tar -cf mongo.tar mongodb.rpm MongoDBIntro.pdf
root@db2a:/tmp# ls -ls Mon* mon*
2688 -rw-r--r-- 1 root root 2750796 Aug 21 16:40 MongoDBIntro.pdf
18472 -rw-r--r-- 1 root root 18913280 Aug 21 16:44 mongo.tar
15780 -rw-r--r-- 1 root root 16157459 Aug 21 16:39 mongodb.rpm
示例2,列出mongo.tar包里所有的文件
root@db2a:/tmp# tar -tvf mongo.tar
-rw-r--r-- root/root 16157459 2017-08-21 16:39 mongodb.rpm
-rw-r--r-- root/root 2750796 2017-08-21 16:40 MongoDBIntro.pdf
示例3,"解压"mongo.tar包:
root@db2a:/tmp# tar -xvf mongo.tar
mongodb.rpm
MongoDBIntro.pdf
示例4,将一个新文件latch.out添加到mongo.tar包中
root@db2a:/tmp# tar -rvf mongo.tar latch.out
latch.out
root@db2a:/tmp# tar -tvf mongo.tar
-rw-r--r-- root/root 16157459 2017-08-21 16:39 mongodb.rpm
-rw-r--r-- root/root 2750796 2017-08-21 16:40 MongoDBIntro.pdf
-rw-r--r-- qingsong/qingsong 13142561 2017-08-20 18:38 latch.out
示例5,将latch.out从mongo.tar包中删除
root@db2a:/tmp# tar --delete -f mongo.tar latch.out
root@db2a:/tmp# tar -tvf mongo.tar
-rw-r--r-- root/root 16157459 2017-08-21 16:39 mongodb.rpm
-rw-r--r-- root/root 2750796 2017-08-21 16:40 MongoDBIntro.pdf
示例6, 打包目录dir2
root@db2a:/tmp# tar -cf dir2.tar dir2/
示例7,将打包好的tar包压缩为*.tar.gz格式:
root@db2a:/tmp# gzip -k dir2.tar
root@db2a:/tmp# ls -l dir2.*
-rw-r--r-- 1 root root 32061440 Aug 21 16:50 dir2.tar
-rw-r--r-- 1 root root 19080496 Aug 21 16:50 dir2.tar.gz
示例8,将目录dir2打包并压缩为*.tar.gz:
root@db2a:/tmp# tar -cvzf dir2.tar.gz dir2/
dir2/
dir2/MongoDBIntro.pdf
dir2/mongodb.rpm
dir2/latch.sh
dir2/latch.out
root@db2a:/tmp# ls -l | grep -i dir2
drwxrwxr-x 2 qingsong qingsong 4096 Aug 21 16:49 dir2
-rw-r--r-- 1 root root 19080487 Aug 21 17:12 dir2.tar.gz
对于gzip,记住一个最重要的参数: "-d"表示解压
对于tar,有三个最重要的参数: "-f"后跟文件名,"-c"表示创建,"-x"表示”解压“,其他的,在需要时参考命令手则即可。