Linux tar Command
last modified October 23, 2026
The tar command in Linux creates, lists, and extracts archive
files. The name stands for tape archive. A tar archive, often called
a tarball, bundles many files and directories into a single file and keeps
their names, permissions, owners, and timestamps. On its own, tar does not
compress data; compression is applied to the whole archive with gzip, bzip2,
xz, or another compressor, which gives files such as .tar.gz or
.tar.xz.
tar is commonly used for backups, software distribution, and
moving directory trees between systems. The examples use GNU tar.
Every tar invocation has one operation mode:
-c (create), -x (extract), -t (list),
-r (append), or -d (compare). The -f
option names the archive file and must be followed by the file name, so it
usually comes last in a group of options such as -cvf.
Creating an archive
We archive a small project directory.
tar -cvf project.tar project/
The -c option creates a new archive, -v lists the
files as they are processed, and -f project.tar sets the archive
name. Directories are archived recursively. The files are stored in the
order in which they are read from the file system.
$ tree project
project
├── docs
│ └── manual.txt
├── logs
│ └── app.log
├── README.md
└── src
├── main.c
└── util.h
4 directories, 5 files
$ tar -cvf project.tar project/
project/
project/logs/
project/logs/app.log
project/README.md
project/docs/
project/docs/manual.txt
project/src/
project/src/util.h
project/src/main.c
Listing the contents
The -t option lists the contents of an archive without
extracting it.
tar -tf project.tar tar -tvf project.tar
With -v, the listing includes permissions, owner and group,
size, and modification time, similar to ls -l. It is a good
habit to check an archive from an unknown source before extracting it.
$ tar -tf project.tar project/ project/logs/ project/logs/app.log project/README.md project/docs/ project/docs/manual.txt project/src/ project/src/util.h project/src/main.c $ tar -tvf project.tar drwxr-xr-x jano/jano 0 2026-10-20 09:30 project/ drwxr-xr-x jano/jano 0 2026-10-20 09:30 project/logs/ -rw-r--r-- jano/jano 8292 2026-10-20 09:30 project/logs/app.log -rw-r--r-- jano/jano 29 2026-10-20 09:30 project/README.md drwxr-xr-x jano/jano 0 2026-10-20 09:30 project/docs/ -rw-r--r-- jano/jano 38893 2026-10-20 09:30 project/docs/manual.txt drwxr-xr-x jano/jano 0 2026-10-20 09:30 project/src/ -rw-r--r-- jano/jano 19 2026-10-20 09:30 project/src/util.h -rw-r--r-- jano/jano 33 2026-10-20 09:30 project/src/main.c
Extracting an archive
The -x option extracts files. By default, they are written to
the current directory; the -C option changes the target
directory.
mkdir restore tar -xvf project.tar -C restore
The archive is extracted into the restore directory, which must
exist. Existing files with the same names are overwritten.
$ mkdir restore $ tar -xvf project.tar -C restore project/ project/logs/ project/logs/app.log project/README.md project/docs/ project/docs/manual.txt project/src/ project/src/util.h project/src/main.c $ ls restore/project docs logs README.md src
Compressed archives
Compression is enabled with -z for gzip, -j for
bzip2, and -J for xz.
tar -czf project.tar.gz project/ tar -cjf project.tar.bz2 project/ tar -cJf project.tar.xz project/
gzip is the fastest and most widely supported, while xz usually produces the
smallest files at the cost of more CPU time. Our sample data is highly
repetitive, so the compression ratios are better than usual. The
-a option chooses the compressor from the archive suffix, for
example tar -caf project.tar.xz project/.
$ tar -czf project.tar.gz project/ $ tar -cjf project.tar.bz2 project/ $ tar -cJf project.tar.xz project/ $ ls -l project.tar* -rw-r--r-- 1 jano jano 61440 Oct 23 10:41 project.tar -rw-r--r-- 1 jano jano 6029 Oct 23 10:41 project.tar.bz2 -rw-r--r-- 1 jano jano 10968 Oct 23 10:41 project.tar.gz -rw-r--r-- 1 jano jano 2028 Oct 23 10:41 project.tar.xz
Extracting compressed archives
When reading an archive from a file, GNU tar detects the compression automatically, so the compression option can be omitted.
tar -tzf project.tar.gz | head -n 3 tar -xf project.tar.xz -C restore
The first command lists a gzip archive with an explicit -z; the
second extracts an xz archive with plain -xf. The compression
option is still required when tar reads the archive from standard input.
$ tar -tzf project.tar.gz | head -n 3 project/ project/logs/ project/logs/app.log $ rm -rf restore/project $ tar -xf project.tar.xz -C restore $ ls restore/project docs logs README.md src
Extracting specific files
Member names given after the archive name limit the extraction to those files.
tar -xvf project.tar project/README.md project/src/main.c tar -xvf project.tar --wildcards "*.h"
The names must match the paths stored in the archive, as shown by
tar -tf. The --wildcards option enables shell-style
patterns; the pattern is quoted so that the shell does not expand it.
$ tar -xvf project.tar project/README.md project/src/main.c project/README.md project/src/main.c $ tar -xvf project.tar --wildcards "*.h" project/src/util.h
Excluding files
The --exclude option skips files and directories matching a
pattern when creating an archive.
tar -czvf source.tar.gz --exclude='*.log' --exclude=project/docs project/
The log file and the whole documentation directory are left out. The
--exclude-vcs option skips version control directories such as
.git.
$ tar -czvf source.tar.gz --exclude='*.log' --exclude=project/docs project/ project/ project/logs/ project/README.md project/src/ project/src/util.h project/src/main.c
Appending files
The -r option appends files to the end of an existing
archive.
tar -rvf project.tar CHANGES.txt
The new file is added at the end of project.tar. Appending works
only with uncompressed archives; for a compressed one, tar reports an error
and the archive must be recreated.
$ echo "v1.0 released" > CHANGES.txt $ tar -rvf project.tar CHANGES.txt CHANGES.txt $ tar -tf project.tar | tail -n 2 project/src/main.c CHANGES.txt $ tar -rf project.tar.gz CHANGES.txt tar: Cannot update compressed archives tar: Error is not recoverable: exiting now
Comparing an archive with the file system
The -d option compares the archive members with the
corresponding files on disk.
tar -df project.tar
After we modify util.h, tar reports the differences in its
size and modification time. Unchanged files produce no output.
$ echo "int x;" >> project/src/util.h $ tar -df project.tar project/src/util.h: Mod time differs project/src/util.h: Size differs
Stripping leading directories
The --strip-components option removes leading path components
from the member names during extraction.
tar -xzf project.tar.gz -C release --strip-components=1
The top-level project/ directory is dropped, and its contents
are extracted directly into release. This is handy for source
tarballs that contain a versioned top-level directory.
$ mkdir release $ tar -xzf project.tar.gz -C release --strip-components=1 $ ls release docs logs README.md src
Using tar in pipelines
A file name of - makes tar write the archive to standard
output or read it from standard input.
tar -cf - project/src | tar -xvf - -C copy
The first tar writes an archive of the src directory to the
pipe, and the second one extracts it into the copy directory.
The same technique copies a directory tree to another machine over SSH, for
example tar -czf - project | ssh user@server "tar -xzf - -C /backup".
$ mkdir copy $ tar -cf - project/src | tar -xvf - -C copy project/src/ project/src/util.h project/src/main.c
Best practices for tar
- Inspect before extracting: Use
tar -tfto see what an archive contains and where files will be written. - Use -C: Extract into a dedicated directory to avoid overwriting files.
- Pick the compressor: Use gzip for speed and compatibility and xz for the smallest size.
- Use relative paths: Archive directories relative to a
parent directory, for example with
-C /home/user project. - Verify backups: Use
tar -dfor a test extraction to check that a backup archive is complete.
Source
In this article, we have used the tar command to create, list,
extract, compress, and update archives, and to copy directory trees through
pipes.
Author
List all Linux tutorials.