tar Command: Create, List and Extract Archives on Linux
Learn how to create, list and extract tar archives with gzip, xz or zstd compression, and verify each step. Every command is tested on four distributions.
Contents
tar bundles many files into one archive and can compress it on the way. This guide shows how to create an archive, list its contents without extracting, and unpack it again, using gzip, xz or zstd compression, with a check after each step.
The three basic operations
GNU tar works in operations. --create (-c) makes an archive, --list (-t) shows what is inside, and --extract (-x) unpacks it. The -f option names the archive file. Add -v to print each file as it is processed.
Start with a small test directory:
mkdir -p /tmp/demo/project/docs
echo "hello" > /tmp/demo/project/readme.txt
echo "manual" > /tmp/demo/project/docs/manual.txtCreate a plain, uncompressed archive. The -C option changes into /tmp/demo before reading project, so the archive stores the relative name project instead of the full path:
tar -cvf /tmp/demo/project.tar -C /tmp/demo projectNote that -C does not affect -f: the archive is still written to the path you gave, not into the -C directory.
GNU tar refuses to create an archive when you give it no files, which protects you from accidentally producing an empty one.
List the contents before extracting
Listing is a safe way to see what an archive holds:
tar -tf /tmp/demo/project.tarproject/
project/readme.txt
project/docs/
project/docs/manual.txtThe order of entries can differ on your system.
Extract into a chosen directory
Use -C again, this time to choose where the files land. The target directory must exist:
mkdir -p /tmp/demo/restored
tar -xf /tmp/demo/project.tar -C /tmp/demo/restored
cat /tmp/demo/restored/project/docs/manual.txtCompress with gzip, xz or zstd
Each compression program has its own option. According to the GNU tar manual, -z filters the archive through gzip, -J through xz, and --zstd through zstd. There is also -j for bzip2.
| Compression | Option | Typical suffix |
|---|---|---|
| gzip | -z | .tar.gz, .tgz |
| xz | -J | .tar.xz |
| zstd | --zstd | .tar.zst |
gzip is normally present already. xz and zstd may need installing. On Debian and Ubuntu:
apt-get update
apt-get install -y xz-utils zstdOn Fedora:
dnf install -y xz zstdOn Arch Linux:
pacman -Sy --noconfirm xz zstdNow create one archive of each kind:
cd /tmp/demo
tar -czf project.tar.gz project
tar -cJf project.tar.xz project
tar --zstd -cf project.tar.zst project
ls -l project.tar*Let tar pick the compressor from the file name
With -a (--auto-compress), tar selects the program from the archive suffix: .gz and .tgz use gzip, .xz uses xz, and .zst and .tzst use zstd. This saves you remembering the option letters:
cd /tmp/demo
tar -caf auto.tar.xz project
tar -tf auto.tar.xzExtract compressed archives
When reading a file, you do not need a compression option. The manual says GNU tar recognizes the format from signature bytes at the start of the file and falls back to the file name suffix if that fails:
cd /tmp/demo
for f in project.tar.gz project.tar.xz project.tar.zst; do
rm -rf out && mkdir out
tar -xf "$f" -C out
echo "$f: $(cat out/project/readme.txt)"
doneThe exception is reading from a pipe or a non-seekable device. There tar cannot look ahead, so it asks for the matching option, for example -z.
Verify an archive against the disk
The --compare (--diff, -d) operation compares archive members with the files in the file system. Run it from the directory the archive was created from. No output means no differences:
cd /tmp/demo
tar -df project.tar.gz
echo "exit status: $?"To change a file and see the difference reported:
cd /tmp/demo
echo "changed" >> project/readme.txt
tar -df project.tar.gz || echo "differences found"Add files to an existing archive
The --append (-r) operation adds new entries to an archive that already exists. The example uses a plain, uncompressed archive:
cd /tmp/demo
echo "extra" > extra.txt
tar -rf project.tar extra.txt
tar -tf project.tar | tail -n 1Common mistakes
- Putting
-fin the middle.-ftakes the next word as the archive name, and the manual warns that a strayfwithout its argument can destroy a file you meant to keep. Write-cvfso the archive name comes right afterf. - Archiving absolute paths. Use
-Cas above so members have short, relative names. - Extracting untrusted archives in a busy directory. List first with
-t, and extract into an empty directory with-C.
Quick reference
| Task | Command |
|---|---|
| Create plain archive | tar -cvf out.tar DIR |
| Create gzip / xz / zstd | tar -czf out.tar.gz DIR, tar -cJf out.tar.xz DIR, tar --zstd -cf out.tar.zst DIR |
| Pick compressor by suffix | tar -caf out.tar.xz DIR |
| List contents | tar -tf out.tar.gz |
| Extract into a directory | tar -xf out.tar.gz -C TARGET |
| Compare with disk | tar -df out.tar.gz |
To check how much space the archives use, see Check disk usage on Linux with df, du and ncdu.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.