Archives and compression: tar, gzip, zstd

Linux · Practical · 9 min

Backups, releases and file transfers are almost always tarballs.

tar bundles files and directories, with permissions and timestamps, into one archive; a compressor then shrinks it. The flags read like a sentence:

  • c create, x extract, t list the contents,
  • f file the archive file name (put it last in the flag group),
  • z gzip, J xz, --zstd zstd, or a to choose by the file extension,
  • v verbose, -C dir change into a directory first.

So tar -czf backup.tar.gz dir/ creates, tar -tzf backup.tar.gz lists, and tar -xzf backup.tar.gz -C /restore extracts into a directory. Modern GNU tar detects compression automatically on extraction, so tar -xf is usually enough. List before extracting archives from elsewhere: some contain absolute paths or a pile of files without a top-level directory.

Compressors trade speed for size: gzip is universal, xz is small but slow, zstd is fast with a good ratio and has become the modern default for many projects. Single files can be compressed directly: gzip file replaces it with file.gz (-k keeps the original), gunzip or zcat reverse it.

zip and unzip handle the format common on Windows. For copying directories between machines, rsync -a or tar piped through ssh preserve metadata.

Example

cd "$(mktemp -d)"
mkdir -p site/css && echo '<h1>hi</h1>' > site/index.html && echo 'body{}' > site/css/a.css
tar -czf site.tar.gz site/          # c=create z=gzip f=file
tar -tzf site.tar.gz | sort         # t=list:  site/  site/css/  site/css/a.css  site/index.html
mkdir restore && tar -xf site.tar.gz -C restore   # x=extract (compression auto-detected)
ls restore/site                     # css  index.html
tar -caf site.tar.xz site/          # -a: compression chosen from the extension
echo 'log line' > app.log
gzip app.log && ls app.log*         # app.log.gz  (original replaced)
zcat app.log.gz                     # log line
gunzip app.log.gz && ls app.log*    # app.log
# tar -cf - site/ | ssh backup 'tar -xf - -C /srv'   # copy a tree over ssh

Remember

  • tar -czf create, -tzf list, -xf extract, -C target directory
  • gzip is universal, zstd fast, xz small
  • List untrusted archives before extracting them

Watch out for

  • Extracting an unknown archive in your home directory and getting hundreds of loose files.

Check yourself

  1. Which command lists the contents of backup.tar.gz without extracting?
  2. What happens to app.log after `gzip app.log`?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man7.org/linux/man-pages/man1/tar.1.html

Builds on

Unlocks

Nothing builds on this yet.