Mac OS Darwin
tar
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
libarchive-formats.
First argument is mode :
-c Create archive
| -t Table of contents is listed to stdout.
| -x Extract from the archive.
| -r Refresh, new entries are appended to the archive.
Only on uncompressed archives stored in regular files. -f is required.
| -u 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.
| | | | | |
When creating or updating an archive, files or directories are added in the order specified.
By default the contents of directories are archived (recursion).
In ex
tract or t
list mode, the entire command line is read and parsed before the archive is opened.
pathnames
or patterns
on the command line indicate which items in the archive should be processed.
Patterns are shell-style globbing patterns as documented in tcsh
.
Options
-v verbose
For create and extract , lists file as processed.
For t able of contents, produces ls -l style output.
| -f file file is the archive.
To use input via pipe or standard input or to send output via pipe or standard output use - .
| -C directory changes directory before adding the files.
In ex tract mode, change directories before extracting entries .
| -H Symbolic links named on the command line are followed; the target of the link is archived.
| -L -h All symbolic links are followed. the target of the link is archived.
| --check-links -l Issue a warning message unless all links to each file are archived.
| --nodump Honor the nodump file flag by skipping this file.
|
Create and Refresh specific options
|
---|
| --newer date Only include files and directories newer than the specified date. This compares ctime
| --newer-mtime date compares mtimes
| --newer-than file Only include files and directories newer than the specified file. This compares ctime
| --newer-mtime-than file compares mtime
|
|
Create specific options
|
---|
-j -y Compress with bzip2
| -z Compress with gzip
| -Z Compress with compress
|
Create, Refresh and Update specific options
|
---|
--one-file-system Do not cross mount points.
| -n Do not recursively archive the contents of directories.
| --format format
cpio , pax , shar , or ustar .
In r efresh and u pdate modes, format must be compatible with the existing archive .
| -o --format ustar
|
@archivearchive 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.
In contrast,
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
|
---|
-k keep existing files, even if older.
If an archive has been r efreshed or u pdated, a file which occurs more than once, later copies will not overwrite earlier copies, not likely what you want!
| --keep-newer-files Do not overwrite existing files that are newer than the versions in the archive.
| -p 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.
| -o 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.
| -m modification times are not preserved. Default: Preserved.
| --numeric-owner numeric UID and GID are used, not named.
Warning restoring to a different system or with a significantly differentdifferent /etc/passwd can corrupt ownership.
| --chroot to the current directory after processing -C before extracting files.
| -S Sparse files are created for blocks containing only NULL bytes, seek over it otherwise. See dd conv=sparse .
| --strip-components n n Leading path elements are removed.
Example: With n of 2; files from /home/secretProject/version2/… can be restored to version3/…
The pathname is edited after checking inclusion/exclusion patterns but before security checks.
Pathnames with fewer elements are silently skipped.
|
--exclude pattern -X files or directories matching the pattern.
Exclusions take precedence over patterns or filenames on the command line.
| --include pattern
Process only files or directories that match pattern
--exclude take precedence over --include
The --include useful when filtering archives. For example, the command
tar -c -f new.tar --include='*foo*' @old.tgz
Creates new.tar containing only the entries from old.tgz containing the string foo .
| -I filename
-T Include patterns in filename
Special "name" of -C causes a chdir to the directory specified by the next line in the list.
Example list:main.c
util.c
lib.c
-C
obj
main.o
util.o
lib.o
Names are terminated by 
Using --null for terminators, also disables the -C lines.
| -P 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.
| -O†
Files are Output to stdout (perhaps piped to another command).
Table of Contents (-t ) listing is written to stderr . Default: stdout .
| -q
--fast-read
Extract or list the first entry that matches pattern .
When all patterns/filenames are matched processing stops.
If the archive was r efreshed 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 r efreshed )
and later entries overwrite earlier entries.
| -s sedcmdSubstitue characters in names per sedcmd of the form:
/old/repl/[g][p][s] .
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
| -U 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.
| --use-compress-program pgm
Pipe through pgm Default: builtin compression
| --options key=value
passed to modules that handle particular formats
key enabled in every module that supports it. Equivalent to key=1 .
| !key disable
| module:key=value
module:key
module:!key | provided only to modules whose name matches module.
| iso9660:!joliet
!joliet disable
| iso9660:!rockridgei
!rockridge disable
| mtree:keyword 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
mtree:all Enables all keywords. mtree:!all to disable all keywords.
| mtree:use-set Enable generation of /set lines in the output.
| mtree:indent Produce human-readable output by indenting options and splitting lines to fit into 80 columns.
| gzip:compression-level decimal integer 0-9 compression level.
| xz:compression-level
| zip:compression=type store (uncompressed) and deflate (gzip ).
| | | | | | | | | | | | |
| -w Ask for confirmation for every action.
| --null 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
| --version display versions and exit.
| -b blocksize 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.
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
ENVIRONMENT
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.
EXIT STATUS
The tar utility exits 0 on success, and >0 if an error occurs.
EXAMPLES
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
command line
tar -c -f new.tar foo1 @old.tgz -C/tmp foo2
will create 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
#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
and --newer-mtime
accept a variety of common date and time specifications, examples include:
12 Mar 2005 7:14:29pm
, 2005-03-12 19:14
, 5 minutes ago
, and 19:14 PST May 1
.
COMPATIBILITY
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.
SECURITY
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:
- 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.
If -U
is specified, any intermediate symlink will also be unconditionally removed.
If neither -U
nor -P
is specified, tar will refuse to extract the entry.
To protect yourself, you should be wary of any archives that come from untrusted sources. You should
examine the contents of an archive with
tar -tf filename
before extraction.
Use -k
to ensure that tar will not overwrite any existing
files or -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.
SEE ALSO
bzip2(1), compress(1), cpio(1), gzip(1), mt(1), pax(1), shar(1), libarchive(3), libarchive-formats(5),
tar(5)
STANDARDS
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.
BUGS
This program follows ISO/IEC 9945-1:1996 (POSIX.1
) for the definition of the -l
option,
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.
The r
and 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.
Uses libarchive-formats.
If it the file to be extracted from an archive is not restored be sure to specify the complete pathname