tar命令是一种用于归档和备份文件的工具,它可以将多个文件和目录打包成一个归档文件,也可以从归档文件中提取文件。
命令格式:
tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG…]
UNIX-style usage
tar -A [OPTIONS] ARCHIVE ARCHIVE
tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
tar -d [-f ARCHIVE] [OPTIONS] [FILE...]
tar -t [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar -r [-f ARCHIVE] [OPTIONS] [FILE...]
tar -u [-f ARCHIVE] [OPTIONS] [FILE...]
tar -x [-f ARCHIVE] [OPTIONS] [MEMBER...]
GNU-style usage
tar {–catenate|–concatenate} [OPTIONS] ARCHIVE ARCHIVE
tar --create [--file ARCHIVE] [OPTIONS] [FILE...]
tar {--diff|--compare} [--file ARCHIVE] [OPTIONS] [FILE...]
tar --delete [--file ARCHIVE] [OPTIONS] [MEMBER...]
tar --append [-f ARCHIVE] [OPTIONS] [FILE...]
tar --list [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar --test-label [--file ARCHIVE] [OPTIONS] [LABEL...]
tar --update [--file ARCHIVE] [OPTIONS] [FILE...]
tar --update [-f ARCHIVE] [OPTIONS] [FILE...]
tar {--extract|--get} [-f ARCHIVE] [OPTIONS] [MEMBER...]
选项:
-c:创建一个新的归档文件
-x:从归档文件中提取文件
-t:查看归档文件中的文件列表
-v:显示详细的处理信息
-f:指定归档文件的
The options listed in the table below tell GNU tar what operation it is to perform. Exactly one of them must
be given. Meaning of non-optional arguments depends on the operation mode requested.
-A, --catenate, --concatenate
Append archive to the end of another archive. The arguments are treated as the names of archives to
append. All archives must be of the same format as the archive they are appended to, otherwise the
resulting archive might be unusable with non-GNU implementations of tar. Notice also that when more
than one archive is given, the members from archives other than the first one will be accessible in the
resulting archive only if using the -i (--ignore-zeros) option.
Compressed archives cannot be concatenated.
-c, --create
Create a new archive. Arguments supply the names of the files to be archived. Directories are
archived recursively, unless the --no-recursion option is given.
-d, --diff, --compare
Find differences between archive and file system. The arguments are optional and specify archive mem‐
bers to compare. If not given, the current working directory is assumed.
--delete
Delete from the archive. The arguments supply names of the archive members to be removed. At least
one argument must be given.
This option does not operate on compressed archives. There is no short option equivalent.
-r, --append
Append files to the end of an archive. Arguments have the same meaning as for -c (--create).
-t, --list
List the contents of an archive. Arguments are optional. When given, they specify the names of the
members to list.
--test-label
Test the archive volume label and exit. When used without arguments, it prints the volume label (if
any) and exits with status 0. When one or more command line arguments are given. tar compares the
volume label with each argument. It exits with code 0 if a match is found, and with code 1 otherwise.
No output is displayed, unless used together with the -v (--verbose) option.
There is no short option equivalent for this option.
-u, --update
Append files which are newer than the corresponding copy in the archive. Arguments have the same mean‐
ing as with -c and -r options.
-x, --extract, --get
Extract files from an archive. Arguments are optional. When given, they specify names of the archive
members to be extracted.
--show-defaults
Show built-in defaults for various tar options and exit. No arguments are allowed.
-?, --help
Display a short option summary and exit. No arguments allowed.
--usage
Display a list of available options and exit. No arguments allowed.
--version
Print program version and copyright information and exit.
例1,打包文件
root@DESKTOP:~# tar cf /tmp/etc-backup.tar /etc
tar: Removing leading `/' from member names
root@DESKTOP:~# ls /tmp/
etc-backup.tar
root@DESKTOP:~# ls -l /tmp/
total 3072
-rw-r--r-- 1 study study 3133440 Jun 22 23:14 etc-backup.tar
例2,打包并同时压缩文件(gzip格式压缩)
root@DESKTOP:~# tar cfz /tmp/etc-backup.tar.gz /etc
tar: Removing leading `/' from member names
root@DESKTOP:~# ls -lh /tmp
total 3.8M
-rw-r--r-- 1 study study 3.0M Jun 22 23:14 etc-backup.tar
-rw-r--r-- 1 root root 518K Jun 22 23:17 etc-backup.tar.gz
例3,打包并同时压缩文件(bzip2格式压缩)
root@DESKTOP:~# tar cjf /tmp/etc-backup.tar.bz2 /etc
tar: Removing leading `/' from member names
root@DESKTOP:~# ls -lh /tmp
total 4.4M
-rw-r--r-- 1 study study 3.0M Jun 22 23:14 etc-backup.tar
-rw-r--r-- 1 root root 497K Jun 22 23:19 etc-backup.tar.bz2
-rw-r--r-- 1 root root 518K Jun 22 23:17 etc-backup.tar.gz
例4,解压文件
root@DESKTOP:~# ls /tmp
etc-backup.tar etc-backup.tar.bz2 etc-backup.tar.gz
root@DESKTOP:~# tar xf /tmp/etc-backup.tar -C /home/study
root@DESKTOP:~# ls -l /home/study
total 0
drwxr-xr-x 1 study study 512 Jun 22 22:33 a
drwxr-xr-x 1 root root 512 Jun 22 23:13 etc
例5,解压gz或bzip2文件
加压gz文件使用的命令 zxf
加压bzip2文件使用的命令 jxf
如果要单独进行 压缩与解压缩,可以使用两个命令:gzip bzip2