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.

Tools Published 4 min read

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:

✓ tested · debian,ubuntu,fedora,arch
mkdir -p /tmp/demo/project/docs
echo "hello" > /tmp/demo/project/readme.txt
echo "manual" > /tmp/demo/project/docs/manual.txt

Create 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:

✓ tested · debian,ubuntu,fedora,arch
tar -cvf /tmp/demo/project.tar -C /tmp/demo project

Note 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:

✓ tested · debian,ubuntu,fedora,arch
tar -tf /tmp/demo/project.tar
project/
project/readme.txt
project/docs/
project/docs/manual.txt

The 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:

✓ tested · debian,ubuntu,fedora,arch
mkdir -p /tmp/demo/restored
tar -xf /tmp/demo/project.tar -C /tmp/demo/restored
cat /tmp/demo/restored/project/docs/manual.txt

Compress 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.

CompressionOptionTypical 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:

✓ tested · debian,ubuntu
apt-get update
apt-get install -y xz-utils zstd

On Fedora:

✓ tested · fedora
dnf install -y xz zstd

On Arch Linux:

✓ tested · arch
pacman -Sy --noconfirm xz zstd

Now create one archive of each kind:

✓ tested · debian,ubuntu,fedora,arch
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:

✓ tested · debian,ubuntu,fedora,arch
cd /tmp/demo
tar -caf auto.tar.xz project
tar -tf auto.tar.xz

Extract 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:

✓ tested · debian,ubuntu,fedora,arch
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)"
done

The 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:

✓ tested · debian,ubuntu,fedora,arch
cd /tmp/demo
tar -df project.tar.gz
echo "exit status: $?"

To change a file and see the difference reported:

✓ tested · debian,ubuntu,fedora,arch
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:

✓ tested · debian,ubuntu,fedora,arch
cd /tmp/demo
echo "extra" > extra.txt
tar -rf project.tar extra.txt
tar -tf project.tar | tail -n 1

Common mistakes

  • Putting -f in the middle. -f takes the next word as the archive name, and the manual warns that a stray f without its argument can destroy a file you meant to keep. Write -cvf so the archive name comes right after f.
  • Archiving absolute paths. Use -C as 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

TaskCommand
Create plain archivetar -cvf out.tar DIR
Create gzip / xz / zstdtar -czf out.tar.gz DIR, tar -cJf out.tar.xz DIR, tar --zstd -cf out.tar.zst DIR
Pick compressor by suffixtar -caf out.tar.xz DIR
List contentstar -tf out.tar.gz
Extract into a directorytar -xf out.tar.gz -C TARGET
Compare with disktar -df out.tar.gz

To check how much space the archives use, see Check disk usage on Linux with df, du and ncdu.

Sources

  1. GNU tar manual: Basic tar operations
  2. GNU tar manual: Creating and reading compressed archives
  3. GNU tar manual: Operations (append, compare)
  4. GNU tar manual: Changing the working directory

#Tar #Gzip #Xz #Zstd #Archives

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.