Archives and Compression: tar, gzip and zip

Intermediate
11 min

Archives and Compression: tar, gzip and zip

Moving a directory tree between machines, publishing a release, or keeping a dated backup all start with turning many files into one and making it smaller. On Linux those are two separate steps: tar bundles files into an archive, and a compressor such as gzip, xz or zstd shrinks it. zip does both at once and is the format Windows users expect. This lesson teaches the commands you will use for each, how to look inside an archive before extracting, and how to choose a compressor.

Archiving vs Compressing

tar (tape archive) concatenates files with their paths, permissions, ownership and timestamps into one stream. It does not compress. A .tar.gz (or .tgz) is a tar archive piped through gzip. Keeping the steps separate is why you can compress any single file with gzip but need tar to handle a directory.

| Extension | Created with | Notes | |---|---|---| | .tar | tar cf | uncompressed bundle | | .tar.gz, .tgz | tar czf | fast, universal | | .tar.xz | tar cJf | smallest, slowest; kernel and source releases | | .tar.zst | tar --zstd -cf | fast with good ratio; modern default in some distros | | .gz, .xz, .zst | gzip, xz, zstd | a single compressed file | | .zip | zip -r | archive and compression together; Windows-friendly |

tar: The Four Flags to Remember

c create, x extract, t list, f file. Add v for a running list of names and z/J/--zstd for compression.

bash
tar czf backup.tar.gz project/ # create tar czvf backup.tar.gz project/ # create, verbose tar tzf backup.tar.gz # list contents tar xzf backup.tar.gz # extract into the current directory tar xzf backup.tar.gz -C /opt # extract elsewhere tar xzf backup.tar.gz project/README.md # extract one file tar xf release.tar.xz # modern tar detects compression on extract

The f flag must come last in the cluster because it takes the filename as its argument; tar fcz name will not work. Always list before extracting an archive from an unknown source — some archives spray files into the current directory instead of a subfolder.

Useful tar Options

bash
tar czf site.tar.gz --exclude=node_modules --exclude="*.log" site/ tar czf site.tar.gz --exclude-from=.tarignore site/ tar czf home.tar.gz -C /home alice # archive alice/ without the /home prefix tar xzf app.tar.gz --strip-components=1 # drop the top-level directory tar czf - project/ | ssh backup "cat > project.tar.gz" # stream over SSH, no local file sudo tar czpf etc.tar.gz /etc # p: preserve permissions exactly (root) tar rf logs.tar new.log # append to an uncompressed archive tar --diff -f backup.tar.gz # compare archive with the filesystem

--strip-components=1 is what you want when an archive contains app-1.4.2/ and you would rather have the files directly in ./.

gzip, xz and zstd on Single Files

bash
gzip huge.log # replaces it with huge.log.gz gzip -k huge.log # keep the original gzip -9 huge.log # best ratio, slower (default is 6) gunzip huge.log.gz # or gzip -d zcat huge.log.gz | grep ERROR # read without extracting zless huge.log.gz # page through it xz -k -T0 huge.log # -T0: use all cores unxz huge.log.xz zstd -19 -T0 huge.log # ratio close to xz at several times the speed zstd -d huge.log.zst

Rules of thumb: gzip for logs and anything that must open everywhere, xz for release tarballs where size matters more than time, zstd for backups and large data where both matter. gzip -l and xz -l show compressed versus original size.

zip and unzip

zip is the right choice when the recipient is on Windows or macOS Finder, or when a tool (Java, Android, many web platforms) expects it.

bash
zip -r report.zip report/ # -r recurse into directories zip -r report.zip report/ -x "*.tmp" "*/node_modules/*" zip -e secrets.zip .env # prompt for a password (weak encryption; prefer gpg) unzip -l report.zip # list unzip report.zip # extract here unzip report.zip -d /tmp/report # extract elsewhere unzip -o report.zip # overwrite without asking unzip -q report.zip # quiet

Install with sudo apt install zip unzip if missing. Note that zip does not preserve Unix permissions or ownership as faithfully as tar, which is why Linux-to-Linux transfers use tarballs.

Common Mistakes

  • tar xzf on a .tar.xz with older tar (use xJf); modern GNU tar auto-detects on extract, but not on create.
  • Extracting straight into ~ and scattering hundreds of files; run tar tzf first or extract into a fresh directory.
  • Compressing already-compressed data (JPEG, MP4, .gz); the result is larger and slower.
  • Using zip for a Linux backup and losing symlinks and permission bits.
  • Forgetting -C and creating archives with absolute paths that overwrite system files when extracted as root (GNU tar strips the leading / for this reason, but not .. components).
Quick Quiz
Question 1 of 3

Which command lists the contents of `backup.tar.gz` without extracting it?

Key Takeaways

  • tar bundles; gzip/xz/zstd compress; .tar.gz combines the two and zip does both in one format.
  • Remember c create, x extract, t list, f file (last), plus z or J for the compressor.
  • List (tar tzf) before extracting, and use -C and --strip-components to control where files land.
  • zcat/zless read compressed files in place; -k keeps originals; -T0 uses all cores.
  • Use tarballs for Linux-to-Linux transfers and zip when the recipient is on Windows.

Next lesson: Disks and Mounts: df, du, lsblk and mount — see where disk space goes and attach new storage to the filesystem tree.

Archives and Compression: tar, gzip and zip - Linux & Command Line | CodeYourCraft | CodeYourCraft