Linux 的归档命令 tar
Linux的tar命令可以用来压缩或者解压缩文件。
官方定义为:
tar
- an archiving utility
语法
1 | $ tar optionA [optionsB] filename |
使用该命令时,optionA选项是必须要有的,它告诉tar
要做什么事情,optionsB选项是辅助使用的,可以选用。
其中optionsA主要为:
-c
创建新的档案文件。如果用户想备份一个目录或是一些文件,就要选择这个选项。相当于打包。-x
从档案文件中释放文件。相当于拆包。-t
列出档案文件的内容,查看已经备份了哪些文件。
不过需要注意的是,这三个参数仅仅能存在一个。
辅助选项常用的为:
-z
:是否同时具有 gzip 的属性,有的话压缩文件格式为:filename.tar.gz-j
:是否同时具有 bzip2 的属性,有的话压缩文件格式为:filename.tar.bz2-v
:压缩的过程中显示文件,这个基本都需要带上-p
:使用原文件的原来属性(属性不会依据使用者而变)--exclude FILE
:在压缩的过程中,不要将 FILE 打包!
压缩文件
接下来的命令为把a,b,c,d压缩到文件test.tar.gz中。
1 | $ tar czvf test.tar.gz a b c d |
查看压缩文件的内容
接下来的命令将列出压缩文件的内容,但是不解压,所以可以先确定,再解压不迟
1 | $ tar tzvf test.tar.gz a b c d |
解压文件
接下来就可以解压操作了。
1 | $ tar zxvf test.tar.gz |
-A或–catenate 新增文件到已存在的备份文件。
-b<区块数目>或–blocking-factor=<区块数目> 设置每笔记录的区块数目,每个区块大小为12Bytes。
-B或–read-full-records 读取数据时重设区块大小。
-c或–create 建立新的备份文件。
-C<目的目录>或–directory=<目的目录> 切换到指定的目录。
-d或–diff或–compare 对比备份文件内和文件系统上的文件的差异。
-f<备份文件>或–file=<备份文件> 指定备份文件。
-F<Script文件>或–info-script=<Script文件> 每次更换磁带时,就执行指定的Script文件。
-g或–listed-incremental 处理GNU格式的大量备份。
-G或–incremental 处理旧的GNU格式的大量备份。
-h或–dereference 不建立符号连接,直接复制该连接所指向的原始文件。
-i或–ignore-zeros 忽略备份文件中的0 Byte区块,也就是EOF。
-k或–keep-old-files 解开备份文件时,不覆盖已有的文件。
-K<文件>或–starting-file=<文件> 从指定的文件开始还原。
-l或–one-file-system 复制的文件或目录存放的文件系统,必须与tar指令执行时所处的文件系统相同,否则不予复制。
-L<媒体容量>或-tape-length=<媒体容量> 设置存放每体的容量,单位以1024 Bytes计算。
-m或–modification-time 还原文件时,不变更文件的更改时间。
-M或–multi-volume 在建立,还原备份文件或列出其中的内容时,采用多卷册模式。
-N<日期格式>或–newer=<日期时间> 只将较指定日期更新的文件保存到备份文件里。
-o或–old-archive或–portability 将资料写入备份文件时使用V7格式。
-O或–stdout 把从备份文件里还原的文件输出到标准输出设备。
-p或–same-permissions 用原来的文件权限还原文件。
-P或–absolute-names 文件名使用绝对名称,不移除文件名称前的”/“号。
-r或–append 新增文件到已存在的备份文件的结尾部分。
-R或–block-number 列出每个信息在备份文件中的区块编号。
-s或–same-order 还原文件的顺序和备份文件内的存放顺序相同。
-S或–sparse 倘若一个文件内含大量的连续0字节,则将此文件存成稀疏文件。
-t或–list 列出备份文件的内容。
-T<范本文件>或–files-from=<范本文件> 指定范本文件,其内含有一个或多个范本样式,让tar解开或建立符合设置条件的文件。
-u或–update 仅置换较备份文件内的文件更新的文件。
-U或–unlink-first 解开压缩文件还原文件之前,先解除文件的连接。
-v或–verbose 显示指令执行过程。
-V<卷册名称>或–label=<卷册名称> 建立使用指定的卷册名称的备份文件。
-w或–interactive 遭遇问题时先询问用户。
-W或–verify 写入备份文件后,确认文件正确无误。
-x或–extract或–get 从备份文件中还原文件。
-X<范本文件>或–exclude-from=<范本文件> 指定范本文件,其内含有一个或多个范本样式,让ar排除符合设置条件的文件。
-z或–gzip或–ungzip 通过gzip指令处理备份文件。
-Z或–compress或–uncompress 通过compress指令处理备份文件。
-<设备编号><存储密度> 设置备份用的外围设备编号及存放数据的密度。
–after-date=<日期时间> 此参数的效果和指定”-N”参数相同。
–atime-preserve 不变更文件的存取时间。
–backup=<备份方式>或–backup 移除文件前先进行备份。
–checkpoint 读取备份文件时列出目录名称。
–concatenate 此参数的效果和指定”-A”参数相同。
–confirmation 此参数的效果和指定”-w”参数相同。
–delete 从备份文件中删除指定的文件。
–exclude=<范本样式> 排除符合范本样式的文件。
–group=<群组名称> 把加入设备文件中的文件的所属群组设成指定的群组。
–help 在线帮助。
–ignore-failed-read 忽略数据读取错误,不中断程序的执行。
–new-volume-script=<Script文件> 此参数的效果和指定”-F”参数相同。
–newer-mtime 只保存更改过的文件。
–no-recursion 不做递归处理,也就是指定目录下的所有文件及子目录不予处理。
–null 从null设备读取文件名称。
–numeric-owner 以用户识别码及群组识别码取代用户名称和群组名称。
–owner=<用户名称> 把加入备份文件中的文件的拥有者设成指定的用户。
–posix 将数据写入备份文件时使用POSIX格式。
–preserve 此参数的效果和指定”-ps”参数相同。
–preserve-order 此参数的效果和指定”-A”参数相同。
–preserve-permissions 此参数的效果和指定”-p”参数相同。
–record-size=<区块数目> 此参数的效果和指定”-b”参数相同。
–recursive-unlink 解开压缩文件还原目录之前,先解除整个目录下所有文件的连接。
–remove-files 文件加入备份文件后,就将其删除。
–rsh-command=<执行指令> 设置要在远端主机上执行的指令,以取代rsh指令。
–same-owner 尝试以相同的文件拥有者还原文件。
–suffix=<备份字尾字符串> 移除文件前先行备份。
–totals 备份文件建立后,列出文件大小。
–use-compress-program=<执行指令> 通过指定的指令处理备份文件。
–volno-file=<编号文件> 使用指定文件内的编号取代预设的卷册编号。
For example, the c option requires creating the archive, the v option requests the verbose operation, and the f option takes an argument that sets the name of the archive to operate upon. The following command, written in the traditional style, instructs tar to store all files from the directory /etc into the archive file etc.tar verbosely listing the files being archived: tar cfv a.tar /etc In UNIX or short-option style, each option letter is prefixed with a single dash, as in other command line utilities. If an option takes argument, the argument follows it, either as a separate command line word, or immediately following the option. However, if the option takes an optional argument, the argument must follow the option letter without any intervening whitespace, as in -g/tmp/snar.db. Any number of options not taking arguments can be clustered together after a single dash, e.g. -vkp. Options that take arguments (whether mandatory or optional), can appear at the end of such a cluster, e.g. -vkpf a.tar. The example command above written in the short-option style could look like: tar -cvf a.tar /etc or tar -c -v -f a.tar /etc In GNU or long-option style, each option begins with two dashes and has a meaningful name, consisting of lower-case letters and dashes. When used, the long option can be abbreviated to its initial letters, provided that this does not create ambiguity. Arguments to long options are supplied either as a separate command line word, immediately following the option, or separated from the option by an equals sign with no intervening whitespace. Optional arguments must always use the latter method. Here are several ways of writing the example command in this style: tar --create --file a.tar --verbose /etc or (abbreviating some options): tar --cre --file=a.tar --verb /etc The options in all three styles can be intermixed, although doing so with old options is not encouraged.
Operation mode
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 re‐
sulting 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 re‐
sulting 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. Notice, that newer files don’t replace their old archive copies, but
instead are appended to the end of archive. The resulting archive can thus contain several members of
the same name, corresponding to various versions of the same file.
-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.
OPTIONS
Operation modifiers
–check-device
Check device numbers when creating incremental archives (default).
-g, --listed-incremental=FILE
Handle new GNU-format incremental backups. FILE is the name of a snapshot file, where tar stores addi‐
tional information which is used to decide which files changed since the previous incremental dump and,
consequently, must be dumped again. If FILE does not exist when creating an archive, it will be cre‐
ated and all files will be added to the resulting archive (the level 0 dump). To create incremental
archives of non-zero level N, create a copy of the snapshot file created during the level N-1, and use
it as FILE.
When listing or extracting, the actual contents of FILE is not inspected, it is needed only due to syn‐
tactical requirements. It is therefore common practice to use /dev/null in its place.
--hole-detection=METHOD
Use METHOD to detect holes in sparse files. This option implies --sparse. Valid values for METHOD are
seek and raw. Default is seek with fallback to raw when not applicable.
-G, --incremental
Handle old GNU-format incremental backups.
--ignore-failed-read
Do not exit with nonzero on unreadable files.
--level=NUMBER
Set dump level for created listed-incremental archive. Currently only --level=0 is meaningful: it in‐
structs tar to truncate the snapshot file before dumping, thereby forcing a level 0 dump.
-n, --seek
Assume the archive is seekable. Normally tar determines automatically whether the archive can be
seeked or not. This option is intended for use in cases when such recognition fails. It takes effect
only if the archive is open for reading (e.g. with --list or --extract options).
--no-check-device
Do not check device numbers when creating incremental archives.
--no-seek
Assume the archive is not seekable.
--occurrence[=N]
Process only the Nth occurrence of each file in the archive. This option is valid only when used with
one of the following subcommands: --delete, --diff, --extract or --list and when a list of files is
given either on the command line or via the -T option. The default N is 1.
--restrict
Disable the use of some potentially harmful options.
--sparse-version=MAJOR[.MINOR]
Set version of the sparse format to use (implies --sparse). This option implies --sparse. Valid argu‐
ment values are 0.0, 0.1, and 1.0. For a detailed discussion of sparse formats, refer to the GNU Tar
Manual, appendix D, "Sparse Formats". Using info reader, it can be accessed running the following com‐
mand: info tar 'Sparse Formats'.
-S, --sparse
Handle sparse files efficiently. Some files in the file system may have segments which were actually
never written (quite often these are database files created by such systems as DBM). When given this
option, tar attempts to determine if the file is sparse prior to archiving it, and if so, to reduce the
resulting archive size by not dumping empty parts of the file.
Overwrite control
These options control tar actions when extracting a file over an existing copy on disk.
-k, --keep-old-files
Don't replace existing files when extracting.
--keep-newer-files
Don't replace existing files that are newer than their archive copies.
--keep-directory-symlink
Don't replace existing symlinks to directories when extracting.
--no-overwrite-dir
Preserve metadata of existing directories.
--one-top-level[=DIR]
Extract all files into DIR, or, if used without argument, into a subdirectory named by the base name of
the archive (minus standard compression suffixes recognizable by --auto-compress).
--overwrite
Overwrite existing files when extracting.
--overwrite-dir
Overwrite metadata of existing directories when extracting (default).
--recursive-unlink
Recursively remove all files in the directory prior to extracting it.
--remove-files
Remove files from disk after adding them to the archive.
--skip-old-files
Don't replace existing files when extracting, silently skip over them.
-U, --unlink-first
Remove each file prior to extracting over it.
-W, --verify
Verify the archive after writing it.
Output stream selection
–ignore-command-error
Ignore subprocess exit codes.
--no-ignore-command-error
Treat non-zero exit codes of children as error (default).
-O, --to-stdout
Extract files to standard output.
--to-command=COMMAND
Pipe extracted files to COMMAND. The argument is the pathname of an external program, optionally with
command line arguments. The program will be invoked and the contents of the file being extracted sup‐
plied to it on its standard output. Additional data will be supplied via the following environment
variables:
TAR_FILETYPE
Type of the file. It is a single letter with the following meaning:
f Regular file
d Directory
l Symbolic link
h Hard link
b Block device
c Character device
Currently only regular files are supported.
TAR_MODE
File mode, an octal number.
TAR_FILENAME
The name of the file.
TAR_REALNAME
Name of the file as stored in the archive.
TAR_UNAME
Name of the file owner.
TAR_GNAME
Name of the file owner group.
TAR_ATIME
Time of last access. It is a decimal number, representing seconds since the Epoch. If the ar‐
chive provides times with nanosecond precision, the nanoseconds are appended to the timestamp
after a decimal point.
TAR_MTIME
Time of last modification.
TAR_CTIME
Time of last status change.
TAR_SIZE
Size of the file.
TAR_UID
UID of the file owner.
TAR_GID
GID of the file owner.
Additionally, the following variables contain information about tar operation mode and the archive be‐
ing processed:
TAR_VERSION
GNU tar version number.
TAR_ARCHIVE
The name of the archive tar is processing.
TAR_BLOCKING_FACTOR
Current blocking factor, i.e. number of 512-byte blocks in a record.
TAR_VOLUME
Ordinal number of the volume tar is processing (set if reading a multi-volume archive).
TAR_FORMAT
Format of the archive being processed. One of: gnu, oldgnu, posix, ustar, v7. TAR_SUBCOMMAND A
short option (with a leading dash) describing the operation tar is executing.
Handling of file attributes
–atime-preserve[=METHOD]
Preserve access times on dumped files, either by restoring the times after reading (METHOD=replace,
this is the default) or by not setting the times in the first place (METHOD=system)
--delay-directory-restore
Delay setting modification times and permissions of extracted directories until the end of extraction.
Use this option when extracting from an archive which has unusual member ordering.
--group=NAME[:GID]
Force NAME as group for added files. If GID is not supplied, NAME can be either a user name or numeric
GID. In this case the missing part (GID or name) will be inferred from the current host's group data‐
base.
When used with --group-map=FILE, affects only those files whose owner group is not listed in FILE.
--group-map=FILE
Read group translation map from FILE. Empty lines are ignored. Comments are introduced with # sign
and extend to the end of line. Each non-empty line in FILE defines translation for a single group. It
must consist of two fields, delimited by any amount of whitespace:
OLDGRP NEWGRP[:NEWGID]
OLDGRP is either a valid group name or a GID prefixed with +. Unless NEWGID is supplied, NEWGRP must
also be either a valid group name or a +GID. Otherwise, both NEWGRP and NEWGID need not be listed in
the system group database.
As a result, each input file with owner group OLDGRP will be stored in archive with owner group NEWGRP
and GID NEWGID.
--mode=CHANGES
Force symbolic mode CHANGES for added files.
--mtime=DATE-OR-FILE
Set mtime for added files. DATE-OR-FILE is either a date/time in almost arbitrary format, or the name
of an existing file. In the latter case the mtime of that file will be used.
-m, --touch
Don't extract file modified time.
--no-delay-directory-restore
Cancel the effect of the prior --delay-directory-restore option.
--no-same-owner
Extract files as yourself (default for ordinary users).
--no-same-permissions
Apply the user's umask when extracting permissions from the archive (default for ordinary users).
--numeric-owner
Always use numbers for user/group names.
--owner=NAME[:UID]
Force NAME as owner for added files. If UID is not supplied, NAME can be either a user name or numeric
UID. In this case the missing part (UID or name) will be inferred from the current host's user data‐
base.
When used with --owner-map=FILE, affects only those files whose owner is not listed in FILE.
--owner-map=FILE
Read owner translation map from FILE. Empty lines are ignored. Comments are introduced with # sign
and extend to the end of line. Each non-empty line in FILE defines translation for a single UID. It
must consist of two fields, delimited by any amount of whitespace:
OLDUSR NEWUSR[:NEWUID]
OLDUSR is either a valid user name or a UID prefixed with +. Unless NEWUID is supplied, NEWUSR must
also be either a valid user name or a +UID. Otherwise, both NEWUSR and NEWUID need not be listed in
the system user database.
As a result, each input file owned by OLDUSR will be stored in archive with owner name NEWUSR and UID
NEWUID.
-p, --preserve-permissions, --same-permissions
extract information about file permissions (default for superuser)
--preserve
Same as both -p and -s.
--same-owner
Try extracting files with the same ownership as exists in the archive (default for superuser).
-s, --preserve-order, --same-order
Sort names to extract to match archive
--sort=ORDER
When creating an archive, sort directory entries according to ORDER, which is one of none, name, or in‐
ode.
The default is --sort=none, which stores archive members in the same order as returned by the operating
system.
Using --sort=name ensures the member ordering in the created archive is uniform and reproducible.
Using --sort=inode reduces the number of disk seeks made when creating the archive and thus can consid‐
erably speed up archivation. This sorting order is supported only if the underlying system provides
the necessary information.
Extended file attributes
–acls Enable POSIX ACLs support.
--no-acls
Disable POSIX ACLs support.
--selinux
Enable SELinux context support.
--no-selinux
Disable SELinux context support.
--xattrs
Enable extended attributes support.
--no-xattrs
Disable extended attributes support.
--xattrs-exclude=PATTERN
Specify the exclude pattern for xattr keys. PATTERN is a POSIX regular expression, e.g. --xattrs-ex‐
clude='^user.', to exclude attributes from the user namespace.
--xattrs-include=PATTERN
Specify the include pattern for xattr keys. PATTERN is a POSIX regular expression.
Device selection and switching
-f, –file=ARCHIVE
Use archive file or device ARCHIVE. If this option is not given, tar will first examine the environ‐
ment variable `TAPE’. If it is set, its value will be used as the archive name. Otherwise, tar will
assume the compiled-in default. The default value can be inspected either using the –show-defaults
option, or at the end of the tar –help output.
An archive name that has a colon in it specifies a file or device on a remote machine. The part before
the colon is taken as the machine name or IP address, and the part after it as the file or device path‐
name, e.g.:
--file=remotehost:/dev/sr0
An optional username can be prefixed to the hostname, placing a @ sign between them.
By default, the remote host is accessed via the rsh(1) command. Nowadays it is common to use ssh(1)
instead. You can do so by giving the following command line option:
--rsh-command=/usr/bin/ssh
The remote machine should have the rmt(8) command installed. If its pathname does not match tar's de‐
fault, you can inform tar about the correct pathname using the --rmt-command option.
--force-local
Archive file is local even if it has a colon.
-F, --info-script=COMMAND, --new-volume-script=COMMAND
Run COMMAND at the end of each tape (implies -M). The command can include arguments. When started, it
will inherit tar's environment plus the following variables:
TAR_VERSION
GNU tar version number.
TAR_ARCHIVE
The name of the archive tar is processing.
TAR_BLOCKING_FACTOR
Current blocking factor, i.e. number of 512-byte blocks in a record.
TAR_VOLUME
Ordinal number of the volume tar is processing (set if reading a multi-volume archive).
TAR_FORMAT
Format of the archive being processed. One of: gnu, oldgnu, posix, ustar, v7.
TAR_SUBCOMMAND
A short option (with a leading dash) describing the operation tar is executing.
TAR_FD File descriptor which can be used to communicate the new volume name to tar.
If the info script fails, tar exits; otherwise, it begins writing the next volume.
-L, --tape-length=N
Change tape after writing Nx1024 bytes. If N is followed by a size suffix (see the subsection Size
suffixes below), the suffix specifies the multiplicative factor to be used instead of 1024.
This option implies -M.
-M, --multi-volume
Create/list/extract multi-volume archive.
--rmt-command=COMMAND
Use COMMAND instead of rmt when accessing remote archives. See the description of the -f option,
above.
--rsh-command=COMMAND
Use COMMAND instead of rsh when accessing remote archives. See the description of the -f option,
above.
--volno-file=FILE
When this option is used in conjunction with --multi-volume, tar will keep track of which volume of a
multi-volume archive it is working in FILE.
Device blocking
-b, –blocking-factor=BLOCKS
Set record size to BLOCKSx512 bytes.
-B, --read-full-records
When listing or extracting, accept incomplete input records after end-of-file marker.
-i, --ignore-zeros
Ignore zeroed blocks in archive. Normally two consecutive 512-blocks filled with zeroes mean EOF and
tar stops reading after encountering them. This option instructs it to read further and is useful when
reading archives created with the -A option.
--record-size=NUMBER
Set record size. NUMBER is the number of bytes per record. It must be multiple of 512. It can can be
suffixed with a size suffix, e.g. --record-size=10K, for 10 Kilobytes. See the subsection Size suf‐
fixes, for a list of valid suffixes.
Archive format selection
-H, –format=FORMAT
Create archive of the given format. Valid formats are:
gnu GNU tar 1.13.x format
oldgnu GNU format as per tar <= 1.12.
pax, posix
POSIX 1003.1-2001 (pax) format.
ustar POSIX 1003.1-1988 (ustar) format.
v7 Old V7 tar format.
--old-archive, --portability
Same as --format=v7.
--pax-option=keyword[[:]=value][,keyword[[:]=value]]...
Control pax keywords when creating PAX archives (-H pax). This option is equivalent to the -o option
of the pax(1)utility.
--posix
Same as --format=posix.
-V, --label=TEXT
Create archive with volume name TEXT. If listing or extracting, use TEXT as a globbing pattern for
volume name.
Compression options
-a, –auto-compress
Use archive suffix to determine the compression program.
-I, --use-compress-program=COMMAND
Filter data through COMMAND. It must accept the -d option, for decompression. The argument can con‐
tain command line options.
-j, --bzip2
Filter the archive through bzip2(1).
-J, --xz
Filter the archive through xz(1).
--lzip Filter the archive through lzip(1).
--lzma Filter the archive through lzma(1).
--lzop Filter the archive through lzop(1).
--no-auto-compress
Do not use archive suffix to determine the compression program.
-z, --gzip, --gunzip, --ungzip
Filter the archive through gzip(1).
-Z, --compress, --uncompress
Filter the archive through compress(1).
--zstd Filter the archive through zstd(1).
Local file selection
–add-file=FILE
Add FILE to the archive (useful if its name starts with a dash).
--backup[=CONTROL]
Backup before removal. The CONTROL argument, if supplied, controls the backup policy. Its valid val‐
ues are:
none, off
Never make backups.
t, numbered
Make numbered backups.
nil, existing
Make numbered backups if numbered backups exist, simple backups otherwise.
never, simple
Always make simple backups
If CONTROL is not given, the value is taken from the VERSION_CONTROL environment variable. If it is
not set, existing is assumed.
-C, --directory=DIR
Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all
options that follow.
--exclude=PATTERN
Exclude files matching PATTERN, a glob(3)-style wildcard pattern.
--exclude-backups
Exclude backup and lock files.
--exclude-caches
Exclude contents of directories containing file CACHEDIR.TAG, except for the tag file itself.
--exclude-caches-all
Exclude directories containing file CACHEDIR.TAG and the file itself.
--exclude-caches-under
Exclude everything under directories containing CACHEDIR.TAG
--exclude-ignore=FILE
Before dumping a directory, see if it contains FILE. If so, read exclusion patterns from this file.
The patterns affect only the directory itself.
--exclude-ignore-recursive=FILE
Same as --exclude-ignore, except that patterns from FILE affect both the directory and all its subdi‐
rectories.
--exclude-tag=FILE
Exclude contents of directories containing FILE, except for FILE itself.
--exclude-tag-all=FILE
Exclude directories containing FILE.
--exclude-tag-under=FILE
Exclude everything under directories containing FILE.
--exclude-vcs
Exclude version control system directories.
--exclude-vcs-ignores
Exclude files that match patterns read from VCS-specific ignore files. Supported files are: .cvsig‐
nore, .gitignore, .bzrignore, and .hgignore.
-h, --dereference
Follow symlinks; archive and dump the files they point to.
--hard-dereference
Follow hard links; archive and dump the files they refer to.
-K, --starting-file=MEMBER
Begin at the given member in the archive.
--newer-mtime=DATE
Work on files whose data changed after the DATE. If DATE starts with / or . it is taken to be a file
name; the mtime of that file is used as the date.
--no-null
Disable the effect of the previous --null option.
--no-recursion
Avoid descending automatically in directories.
--no-unquote
Do not unquote input file or member names.
--no-verbatim-files-from
Treat each line read from a file list as if it were supplied in the command line. I.e., leading and
trailing whitespace is removed and, if the resulting string begins with a dash, it is treated as tar
command line option.
This is the default behavior. The --no-verbatim-files-from option is provided as a way to restore it
after --verbatim-files-from option.
This option is positional: it affects all --files-from options that occur after it in, until --verba‐
tim-files-from option or end of line, whichever occurs first.
It is implied by the --no-null option.
--null Instruct subsequent -T options to read null-terminated names verbatim (disables special handling of
names that start with a dash).
See also --verbatim-files-from.
-N, --newer=DATE, --after-date=DATE
Only store files newer than DATE. If DATE starts with / or . it is taken to be a file name; the ctime
of that file is used as the date.
--one-file-system
Stay in local file system when creating archive.
-P, --absolute-names
Don't strip leading slashes from file names when creating archives.
--recursion
Recurse into directories (default).
--suffix=STRING
Backup before removal, override usual suffix. Default suffix is ~, unless overridden by environment
variable SIMPLE_BACKUP_SUFFIX.
-T, --files-from=FILE
Get names to extract or create from FILE.
Unless specified otherwise, the FILE must contain a list of names separated by ASCII LF (i.e. one name
per line). The names read are handled the same way as command line arguments. They undergo quote re‐
moval and word splitting, and any string that starts with a - is handled as tar command line option.
If this behavior is undesirable, it can be turned off using the --verbatim-files-from option.
The --null option instructs tar that the names in FILE are separated by ASCII NUL character, instead of
LF. It is useful if the list is generated by find(1) -print0 predicate.
--unquote
Unquote file or member names (default).
--verbatim-files-from
Treat each line obtained from a file list as a file name, even if it starts with a dash. File lists
are supplied with the --files-from (-T) option. The default behavior is to handle names supplied in
file lists as if they were typed in the command line, i.e. any names starting with a dash are treated
as tar options. The --verbatim-files-from option disables this behavior.
This option affects all --files-from options that occur after it in the command line. Its effect is
reverted by the --no-verbatim-files-from} option.
This option is implied by the --null option.
See also --add-file.
-X, --exclude-from=FILE
Exclude files matching patterns listed in FILE.
File name transformations
–strip-components=NUMBER
Strip NUMBER leading components from file names on extraction.
--transform=EXPRESSION, --xform=EXPRESSION
Use sed replace EXPRESSION to transform file names.
File name matching options
These options affect both exclude and include patterns.
--anchored
Patterns match file name start.
--ignore-case
Ignore case.
--no-anchored
Patterns match after any / (default for exclusion).
--no-ignore-case
Case sensitive matching (default).
--no-wildcards
Verbatim string matching.
--no-wildcards-match-slash
Wildcards do not match /.
--wildcards
Use wildcards (default for exclusion).
--wildcards-match-slash
Wildcards match / (default for exclusion).
Informative output
–checkpoint[=N]
Display progress messages every Nth record (default 10).
--checkpoint-action=ACTION
Run ACTION on each checkpoint.
--clamp-mtime
Only set time when the file is more recent than what was given with --mtime.
--full-time
Print file time to its full resolution.
--index-file=FILE
Send verbose output to FILE.
-l, --check-links
Print a message if not all links are dumped.
--no-quote-chars=STRING
Disable quoting for characters from STRING.
--quote-chars=STRING
Additionally quote characters from STRING.
--quoting-style=STYLE
Set quoting style for file and member names. Valid values for STYLE are literal, shell, shell-always,
c, c-maybe, escape, locale, clocale.
-R, --block-number
Show block number within archive with each message.
--show-omitted-dirs
When listing or extracting, list each directory that does not match search criteria.
--show-transformed-names, --show-stored-names
Show file or archive names after transformation by --strip and --transform options.
--totals[=SIGNAL]
Print total bytes after processing the archive. If SIGNAL is given, print total bytes when this signal
is delivered. Allowed signals are: SIGHUP, SIGQUIT, SIGINT, SIGUSR1, and SIGUSR2. The SIG prefix can
be omitted.
--utc Print file modification times in UTC.
-v, --verbose
Verbosely list files processed.
--warning=KEYWORD
Enable or disable warning messages identified by KEYWORD. The messages are suppressed if KEYWORD is
prefixed with no- and enabled otherwise.
Multiple --warning messages accumulate.
Keywords controlling general tar operation:
all Enable all warning messages. This is the default.
none Disable all warning messages.
filename-with-nuls
"%s: file name read contains nul character"
alone-zero-block
"A lone zero block at %s"
Keywords applicable for tar --create:
cachedir
"%s: contains a cache directory tag %s; %s"
file-shrank
"%s: File shrank by %s bytes; padding with zeros"
xdev "%s: file is on a different filesystem; not dumped"
file-ignored
"%s: Unknown file type; file ignored"
"%s: socket ignored"
"%s: door ignored"
file-unchanged
"%s: file is unchanged; not dumped"
ignore-archive
"%s: file is the archive; not dumped"
file-removed
"%s: File removed before we read it"
file-changed
"%s: file changed as we read it"
failed-read
Suppresses warnings about unreadable files or directories. This keyword applies only if used to‐
gether with the --ignore-failed-read option.
Keywords applicable for tar --extract:
existing-file
"%s: skipping existing file"
timestamp
"%s: implausibly old time stamp %s"
"%s: time stamp %s is %s s in the future"
contiguous-cast
"Extracting contiguous files as regular files"
symlink-cast
"Attempting extraction of symbolic links as hard links"
unknown-cast
"%s: Unknown file type '%c', extracted as normal file"
ignore-newer
"Current %s is newer or same age"
unknown-keyword
"Ignoring unknown extended header keyword '%s'"
decompress-program
Controls verbose description of failures occurring when trying to run alternative decompressor
programs. This warning is disabled by default (unless --verbose is used). A common example of
what you can get when using this warning is:
$ tar --warning=decompress-program -x -f archive.Z
tar (child): cannot run compress: No such file or directory
tar (child): trying gzip
This means that tar first tried to decompress archive.Z using compress, and, when that failed,
switched to gzip.
record-size
"Record size = %lu blocks"
Keywords controlling incremental extraction:
rename-directory
"%s: Directory has been renamed from %s"
"%s: Directory has been renamed"
new-directory
"%s: Directory is new"
xdev "%s: directory is on a different device: not purging"
bad-dumpdir
"Malformed dumpdir: 'X' never used"
-w, --interactive, --confirmation
Ask for confirmation for every action.
Compatibility options
-o When creating, same as –old-archive. When extracting, same as –no-same-owner.
Size suffixes
Suffix Units Byte Equivalent
b Blocks SIZE x 512
B Kilobytes SIZE x 1024
c Bytes SIZE
G Gigabytes SIZE x 1024^3
K Kilobytes SIZE x 1024
k Kilobytes SIZE x 1024
M Megabytes SIZE x 1024^2
P Petabytes SIZE x 1024^5
T Terabytes SIZE x 1024^4
w Words SIZE x 2
RETURN VALUE
Tar exit code indicates whether it was able to successfully perform the requested operation, and if not, what
kind of error occurred.
0 Successful termination.
1 Some files differ. If tar was invoked with the --compare (--diff, -d) command line option, this means
that some files in the archive differ from their disk counterparts. If tar was given one of the --cre‐
ate, --append or --update options, this exit code means that some files were changed while being
archived and so the resulting archive does not contain the exact copy of the file set.
2 Fatal error. This means that some fatal, unrecoverable error occurred.
If a subprocess that had been invoked by tar exited with a nonzero exit code, tar itself exits with that code
as well. This can happen, for example, if a compression option (e.g. -z) was used and the external compressor
program failed. Another example is rmt failure during backup to a remote device.