Mac OS Darwin
manipulate tape archives
tar -c [options] [files | directories]
tar -r | -u -f archive-file [options] [files | directories]
tar -t | -x [options] [patterns]†
tar [bundled-flags args] [file | pattern …]
Creates and manipulates streaming archive files, originally for storage to magnetic tape.
This implementation can extract from tar, pax,
cpio, zip, jar, ar, and ISO 9660 cdrom images and can create tar, pax, cpio, ar, and shar archives see
First argument is mode :
When creating or updating an archive, files or directories are added in the order specified.
|Table of contents is listed to |
| Extract from the archive. |
| Refresh, new entries are appended to the archive. |
Only on uncompressed archives stored in regular files.
-f is required.
| Update an archive, new entries are added if modification date newer than the corresponding entry in the archive. |
Only on uncompressed archives stored in regular files.
-f is required.
By default the contents of directories are archived (recursion).
t list mode, the entire command line is read and parsed before the archive is opened.
patterns on the command line indicate which items in the archive should be processed.
Patterns are shell-style globbing patterns as documented in
For create and extract , lists file as processed.
table of contents, produces
ls -l style output.
file is the archive.
To use input via pipe or standard input or to send output via pipe or standard output use
|changes directory before adding the files. |
xtract mode, change directories before extracting entries .
|Symbolic links named on the command line are followed; the target of the link is archived.
|All symbolic links are followed. the target of the link is archived.
|Issue a warning message unless all links to each file are archived.
| Honor the nodump file flag by skipping this file.
|Create and Refresh specific options
| Only include files and directories newer than the specified date. This compares ctime
| compares mtimes
| Only include files and directories newer than the specified file. This compares ctime
| compares mtime
|Create specific options
|Compress with bzip2
| Compress with gzip
|Compress with compress
|Create, Refresh and Update specific options
| Do not cross mount points.
| Do not recursively archive the contents of directories.
update modes, format must be compatible with the existing archive .
archive is opened and the entries in it is appended to the current archive. Example:
tar -c -f new.tar newfile @original.tar
Creates new.tar containing newfile and all of the files in original.tar.
tar -c -f new.tar newfile original.tar
Creates new.tar with only two entries.
tar -czf - --format pax @-
reads an archive from standard input (whose format is determined automatically) and
creates a gzip-compressed pax-format archive on stdout. Used to convert from one format to another.
|eXtract specific options
|keep existing files, even if older.|
If an archive has been
updated, a file which occurs more than once, later copies will not overwrite earlier copies, not likely what you want!
|Do not overwrite existing files that are newer than the versions in the archive.
| Preserve file permissions: owner, file modes, file flags and ACLs.|
Default: newly-created files are owned by the running user ,
Mode is restored for newly-created files, other entries receive default permissions.
As root, restores the owner unless
-o is also specified.
|ownership is set user and group of the running user rather than the original.|
As root with
-p modes and flags restored, but ACLs or owner information is discarded.
| modification times are not preserved. Default: Preserved.
| numeric UID and GID are used, not named. |
Warning restoring to a different system or with a significantly differentdifferent
/etc/passwd can corrupt ownership.
|to the current directory after processing |
-C before extracting files.
| Sparse files are created for blocks containing only NULL bytes, seek over it otherwise. See |
n Leading path elements are removed.
n of 2; files from
/home/secretProject/version2/… can be restored to
The pathname is edited after checking inclusion/exclusion patterns but before security checks.
Pathnames with fewer elements are silently skipped.
|files or directories matching the pattern. |
Exclusions take precedence over
filenames on the command line.
Process only files or directories that match |
--exclude take precedence over
--include useful when filtering archives. For example, the command
tar -c -f new.tar --include='*foo*' @old.tgz
new.tar containing only the entries from
old.tgz containing the string
| Include |
Special "name" of
-C causes a chdir to the directory specified by the next line in the list.
Names are terminated by ␤
--null for terminators, also disables the
| Preserve pathnames. |
By default, absolute pathnames (those that begin with a / character) have
the leading slash removed both when creating archives and extracting from them.
tar does not extract archive entries whose pathnames contain
.. or whose target directory would be
altered by a symlink. This suppresses these behaviors.
Files are Output to |
stdout (perhaps piped to another command).
Table of Contents (
-t) listing is written to
|Extract or list the first entry that matches |
When all patterns/filenames are matched processing stops.
If the archive was
refreshed there is the possibility that a newer version is not found.
Default: archive is read to the end, as there can be multiple entries with the same name ( if the archive was
and later entries overwrite earlier entries.
|Substitue characters in names per |
sedcmd of the form:
old is a basic regular expression.
~ is substituted with the match, 1 to 9 with the content of the corresponding captured group.
g globally match but stop on the first unmatched pattern.
s Symbolic link's value are used
old for pattern matching.
p print (output) the original path name and the new path name is output to standard error after a successful substitution
|Unlink(remove) files before extracting them. |
Existing hardlinks are broken, as will any symlink that would affect the location of an extracted file.
Default: existing files are overwriten, which preserves existing hardlinks.
Useful if files being extracted are large relative to current free space or
if the archive had been refreshed and multiple copys of files exist.
|Pipe through |
pgm Default: builtin compression
| passed to modules that handle particular formats|
| enabled in every module that supports it. Equivalent to |
|provided only to modules whose name matches module.
| specify which mtree keywords are included in the output.
cksum, device, flags, gid, gname, indent, link, md5, mode, nlink, rmd160,
sha1, sha256, sha384, sha512, size, time, uid, uname
Default : device, flags, gid, gname, link, mode, nlink, size, time, type, uid, uname
|Enables all keywords. |
mtree:!all to disable all keywords.
| Enable generation of /set lines in the output.
| Produce human-readable output by indenting options and splitting lines to fit into 80 columns.
| decimal integer 0-9 compression level.
store (uncompressed) and
deflate (gzip ).
|Ask for confirmation for every action.
| with |
-I, -T, or
-X) Filenames or patterns are separated by null characters, not by newlines. Used to read filenames output by the
find -print0 helpful when files contain spaces
| display versions and exit.
|in 512-byte records, . As a rule, this argument is
only needed when reading from or writing to tape drives, and usually not even then as the
default block size of 20 records (10240 bytes) is very common.
LANG The locale to use. See environ(7) for more information.
TZ The timezone to use when displaying dates. See environ(7) for more information.
The tar utility exits 0 on success, and >0 if an error occurs.
The following creates a new archive called file.tar.gz that contains two files source.c and source.h:
tar -czf file.tar.gz source.c source.h
To view a detailed table of contents for this archive:
tar -tvf file.tar.gz
To examine the contents of an ISO 9660 cdrom image:
tar -tf image.iso
To move file hierarchies, invoke tar as
tar -cf - -C srcdir . | tar -xpf - -C destdir
or more traditionally
cd srcdir ; tar -cf - . | (cd destdir ; tar -xpf -)
In create mode, the list of files and directories to be archived can also include directory change
instructions of the form
-Cfoo/baz and archive inclusions of the form @archive-file. For example, the
tar -c -f new.tar foo1 @old.tgz -C/tmp foo2
new.tar. tar will read the file
foo1 from the current directory and add it
to the output archive. It will then read each entry from
old.tgz and add those entries to the output
archive. Finally, it will switch to the /tmp directory and add
foo2 to the output archive.
An input file in mtree format can be used to create an output archive with arbitrary ownership, permissions, or names that differ from existing data on disk:
$ cat input.mtree
usr/bin uid=0 gid=0 mode=0755 type=dir
usr/bin/ls uid=0 gid=0 mode=0755 type=file content=myls
$ tar -cvf output.tar @input.mtree
--newer-mtime accept a variety of common date and time specifications, examples include:
12 Mar 2005 7:14:29pm,
5 minutes ago, and
19:14 PST May 1.
The bundled-arguments format is supported for compatibility with historic implementations. It consists
of an initial word (with no leading - character) in which each character indicates an option. Arguments follow as separate words. The order of the arguments must match the order of the corresponding
characters in the bundled command word. For example,
tar tbf 32 file.tar
specifies three flags t, b, and f. The b and f flags both require arguments, so there must be two
additional items on the command line. The 32 is the argument to the b flag, and file.tar is the argument to the f flag.
The mode options c, r, t, u, and x and the options b, f, l, m, o, v, and w comply with SUSv2.
For maximum portability, scripts that invoke tar should use the bundled-argument format above, should
limit themselves to the c, t, and x modes, and the b, f, m, v, and w options.
Additional long options are provided to improve compatibility with other tar implementations.
Certain security issues are common to many archiving programs. Carefully-crafted archives can request that tar extract files to locations outside of the target directory.
This can cause unintended overwriting of files.
If the archive is being extracted by the superuser, any file on the system can potentially be overwritten.
This can happen by:
To protect yourself, you should be wary of any archives that come from untrusted sources. You should
examine the contents of an archive with
- Archive entries can have absolute pathnames. By default, tar removes the leading / character
from filenames before restoring them to guard against this problem.
- Archive entries can have pathnames that include .. components. By default, tar will not
extract files containing .. components in their pathname.
- Archive entries can exploit symbolic links to restore files to other directories. An archive
can restore a symbolic link to another directory, then use that link to restore a file into
that directory. To guard against this, tar checks each extracted path for symlinks. If the
final path element is a symlink, it is removed and replaced with the archive entry.
-U is specified, any intermediate symlink will also be unconditionally removed.
-P is specified, tar will refuse to extract the entry.
tar -tf filename
-k to ensure that tar will not overwrite any existing
-U to remove any pre-existing files.
You should generally not extract archives while running with super-user privileges.
-P disables the security checks above and allows you to extract an archive while preserving any absolute pathnames, .. components, or
symlinks to other directories.
bzip2(1), compress(1), cpio(1), gzip(1), mt(1), pax(1), shar(1), libarchive(3), libarchive-formats(5),
There is no current POSIX standard for the tar command; it appeared in ISO/IEC 9945-1:1996
POSIX.1) but was dropped from IEEE Std 1003.1-2001 (
POSIX.1). The options used by this imple-
mentation were developed by surveying a number of existing tar implementations as well as the old POSIX
specification for tar and the current POSIX specification for pax.
The ustar and pax interchange file formats are defined by IEEE Std 1003.1-2001 (
POSIX.1) for the pax command.
This program follows ISO/IEC 9945-1:1996 (
POSIX.1) for the definition of the
GNU tar prior to version 1.15 treated
-l as a synonym for the --one-file-system option.
-C option may differ from historic implementations.
All archive output is written in correctly-sized blocks, even if the output is being compressed.
Whether or not the last output block is padded to a full block size varies depending on the format and
the output device. For tar and cpio formats, the last block of output is padded to a full block size
if the output is being written to standard output or to a character or block device such as a tape
drive. If the output is being written to a regular file, the last block will not be padded. Many compressors, including gzip(1) and bzip2(1), complain about the null padding when decompressing an archive
created by tar, although they still extract it correctly.
u modes require the archive be uncompressed and located in a regular file on disk.
Other archives can be modified using
c mode with the @archive-file extension.
To archive a file called @foo or -foo you must specify it as ./@foo or ./-foo, respectively.
In create mode, a leading
./ is always removed. A leading
/ is stripped unless
-P option is specified.
There needs to be better support for file selection on both create and extract.
There is not yet any support for multi-volume archives or for archiving sparse files.
Converting between dissimilar archive formats (such as tar and cpio) using the @- convention can cause
hard link information to be lost. (This is a consequence of the incompatible ways that different archive formats store hardlink information.)
There are alternative long options for many of the short options.
If it the file to be extracted from an archive is not restored be sure to specify the complete