On Unix-like operating systems, the tar command creates, maintains, modifies, and extracts files that are archived in the tar format.

This page covers the GNU/Linux version of tar.

Description

“Tar” stands for tape archive. It is an archiving file format.

  • Description
  • Syntax
  • Operation
  • Functions
  • Other options
  • Environment
  • Examples
  • Related commands
  • Linux commands help

tar was originally developed in the early days of Unix for the purpose of backing up files to tape-based storage devices. It was later formalized as part of the POSIX standard, and today is used to collect, distribute, and archive files, while preserving file system attributes such as user and group permissions, access and modification dates, and directory structures.

This documentation covers the GNU version of tar, which is included with most modern variants of the Linux operating system.

Syntax

tar [-] A –catenate –concatenate | c –create | d –diff –compare | –delete | r –append | t –list | –test-label | u –update | x –extract –get [options] [pathname …]

Operation

The first argument to tar should be a function specification: either one of the letters A, c, d, r, t, u, or x, or one of the long function names. A function letter does not need to be prefixed with a dash ("-"), and may be combined with other single-letter options. A long function name must be prefixed with a double dash ("–"). Some options take a parameter; with the single-letter form these must be given as separate arguments. With the long form, they may be given by appending “=value” to the option.

For example, the following commands are all equivalent:

tar –create –file=archive.tar file1 file2

tar -c -f archive.tar file1 file2

tar -cf archive.tar file1 file2

tar cf archive.tar file1 file2

Functions

Specifying one of the following functions selects what tar’s main mode of operation will be:

Other options

The following options specify the way tar operates:

Environment

The following environment variables affect the operation of tar:

  • CVS/, and everything under itRCS/, and everything under itSCCS/, and everything under it.git/, and everything under it.gitignore.cvsignore.svn/, and everything under it.arch-ids/, and everything under it{arch}/, and everything under it=RELEASE-ID=meta-update=update.bzr.bzrignore.bzrtags.hg.hgignore.hgrags_darcs

tar -x -f archive.tar –occurrence filename

tar –extract –file archive.tar –strip-components=2

tar cf archive.tar –transform ’s,^./,usr/,'

Examples

Create archive archive.tar containing files file1 and file2. Here, the c tells tar you will be creating an archive; the f tells tar that the next option (here it’s archive.tar) will be the name of the archive it creates. file1 and file2, the final arguments, are the files to be archived.

tar -tvf archive.tar

List the files in the archive archive.tar verbosely. Here, the t tells tar to list the contents of an archive; v tells tar to operate verbosely; and f indicates that the next argument will be the name of the archive file to operate on.

tar -xf archive.tar

Extract the files from archive archive.tar. x tells tar to extract files from an archive; f tells tar that the next argument will be the name of the archive to operate on.

tar -xzvf archive.tar.gz

Extract the files from gzipped archive archive.tar.gz verbosely. Here, the z tells tar that the archive will be compressed with gzip.

tar -cf archive.tar mydir/

Creates an archive of the directory mydir.

tar -czf archive.tar.gz mydir/

Creates an gzip-compressed archive of the directory mydir.

tar -zxvf myfile.tar.gz

Extract the contents of the myfile.tar.gz into the current directory.

tar -xvf archive.tar documents/work/budget.doc

Extract only the file documents/work/budget.doc from the archive archive.tar. Produce verbose output.

tar -xvf archive.tar documents/work/

Extract only the directory documents/work/, and any files it contains, from the archive archive.tar. Produce verbose output.

tar -xvf archive.tar –wildcards ‘*.doc’

Extract only files with the extension .doc from the archive archive.tar. The –wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the file name (.doc) is enclosed in single-quotes to protect the wildcard () from being expanded incorrectly by the shell.

tar -rvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the existing archive archive.tar. The r option is the same as the long option –append.

tar -uvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the archive archive.tar only if it’s newer than the version already in the archive (or does not yet exist in the archive). Here, u is the same as the long option –update.

tar -cf - documents/work/ | wc -c

Estimate the file size of an archive of the directory documents/work, but do not create the file. Here, the archive file is specified as a dash ("-"), which tells tar to send its archived output to the standard output rather than a file on disk. This output is then piped to the wc command, which reports how many bytes (-c) were in the input it received.

tar -czf DogPhotos.tar.gz –exclude=‘kitty.jpg’ MyPetPhotos

Create DogPhotos.tar.gz of all files contained in the MyPetPhotos without the kitty.jpg photo.

tar tf hope.tar.gz | grep myfile.txt

Search the hope.tar.gz file for the file myfile.txt and list the full path of the file. The returned results would resemble the line shown below.

computerhopehope/homedir/public_html/data/myfile.txt

tar -zxvf hope.tar.gz computerhopehope/homedir/public_html/data/myfile.txt

In the above example, the tar command would extract the one file myfile.txt from the hope.tar.gz. The full path to this file was determined using the example shown earlier.

ar — Create, modify, and extract files from archives.basename — Strip directory information and suffixes from file names.cd — Change the working directory.chown — Change the ownership of files or directories.cpio — Copy files to or from archives.dirname — Strip the file name from a pathname, leaving only the directory component.gzip —Create, modify, list the contents of, and extract files from GNU zip archives.ls — List the contents of a directory or directories.mt — Control magnetic tapes.zcat — Print the uncompressed contents of compressed files.