tar — Create and Extract Archives

Practical guide to tar — create and extract archives with gzip, bzip2 and xz compression, backups, SSH transfer and selective extraction.

tar is the go-to tool on Linux and Unix for bundling whole directory trees into a single archive file – preserving permissions, timestamps and symlinks. Originally built for tape drives (hence "tape archive"), it remains the backbone of nearly every backup and software distribution. tar doesn't compress on its own; it hands the data stream off to gzip, bzip2 or xz, which you select with a single extra flag. This guide walks you through the essential commands for packing, extracting, inspecting and building advanced backup workflows.

Create Archives

tar -cf <archive.tar> <files> — Create an uncompressed tar archive.

tar -cf backup.tar /home/user/docs/

tar -czf <archive.tar.gz> <files> — Create a gzip-compressed archive.

tar -czf backup.tar.gz /home/user/docs/

tar -cjf <archive.tar.bz2> <files> — Create a bzip2-compressed archive.

tar -cjf backup.tar.bz2 /home/user/docs/

tar -cJf <archive.tar.xz> <files> — Create an xz-compressed archive (best compression).

tar -cJf backup.tar.xz /home/user/docs/

tar -cf <archive.tar> -C <directory> . — Create archive from a directory without including the parent path.

tar -cf backup.tar -C /home/user/docs/ .

tar -czf <archive.tar.gz> --exclude='<pattern>' <files> — Create archive excluding files matching a pattern.

tar -czf backup.tar.gz --exclude='*.log' /home/user/docs/

Extract Archives

tar -xf <archive.tar> — Extract a tar archive to the current directory.

tar -xf backup.tar

tar -xzf <archive.tar.gz> — Extract a gzip-compressed archive.

tar -xzf backup.tar.gz

tar -xjf <archive.tar.bz2> — Extract a bzip2-compressed archive.

tar -xjf backup.tar.bz2

tar -xJf <archive.tar.xz> — Extract an xz-compressed archive.

tar -xJf backup.tar.xz

tar -xf <archive.tar> -C <directory> — Extract to a specific directory.

tar -xf backup.tar -C /tmp/restore/

tar -xf <archive.tar> <file> — Extract only a specific file from the archive.

tar -xf backup.tar home/user/docs/readme.txt

tar -xf <archive.tar> --wildcards '*.txt' — Extract only files matching a wildcard pattern.

tar -xf backup.tar --wildcards '*.txt'

List Contents

tar -tf <archive.tar> — List all files in an archive without extracting.

tar -tf backup.tar

tar -tzf <archive.tar.gz> — List contents of a gzip-compressed archive.

tar -tzf backup.tar.gz

tar -tvf <archive.tar> — List contents with detailed file info (permissions, size, date).

tar -tvf backup.tar

tar -tf <archive.tar> | grep <pattern> — Search for a specific file in the archive.

tar -tf backup.tar | grep config

Append & Update

tar -rf <archive.tar> <files> — Append files to an existing tar archive (uncompressed only).

tar -rf backup.tar newfile.txt

tar -uf <archive.tar> <files> — Update files in the archive only if they are newer.

tar -uf backup.tar /home/user/docs/

tar --delete -f <archive.tar> <file> — Remove a file from the archive (uncompressed only).

tar --delete -f backup.tar oldfile.txt

Advanced Options

tar -czf - <files> | ssh <host> "cat > backup.tar.gz" — Create a compressed archive and send it to a remote server via SSH.

tar -czf - /home/user/docs/ | ssh server "cat > backup.tar.gz"

tar -czf <archive.tar.gz> --newer-mtime='<date>' <files> — Archive only files modified after a certain date.

tar -czf recent.tar.gz --newer-mtime='2025-01-01' /home/user/docs/

tar -czf <archive.tar.gz> -T <filelist> — Create archive from a list of files in a text file.

tar -czf backup.tar.gz -T files.txt

tar -czf - <files> | split -b <size> - <prefix> — Create a split archive for large backups.

tar -czf - /data/ | split -b 100M - backup_part_

cat <prefix>* | tar -xzf - — Extract a split archive.

cat backup_part_* | tar -xzf -

Conclusion

tar is remarkably versatile: a single tool for backups, software packages and shipping entire directory trees across the network. Remember the two letter pairs -c (create) and -x (extract) plus the compression switch (z for gzip, j for bzip2, J for xz) and you cover the daily essentials. When creating archives, tar strips leading / from absolute paths, so archives extract relative by default. Still, be cautious when unpacking archives from untrusted sources: inspect them first with -t, since crafted paths (path traversal via ../) could overwrite files outside the target directory.

Further Reading

  • 7z – high-compression archiver with its own 7z format
  • gzip – fast stream compression, often paired with tar
  • zip – cross-platform archive format with per-file compression