Command Palette

Search for a command to run...

Hectal
PHASE 1Beginner ~7 min· topic 7 of 7

Topic 1.7

Archives & Compression: tar, gzip, zip

In one line

Backups, releases, and log bundles are shipped as archives. tar bundles files into one; gzip, xz, and zstd shrink them. Know the handful of flags that cover almost every real case.

0/7 · 0%

Think of it like this

Moving house. tar puts many items into one box (an archive); compression (gzip, zstd) vacuum-packs the box so it takes less space. You can box without vacuum-packing, but never vacuum-pack without the box when there are many items.

Key ideas

  1. 01

    CREATE: tar -czf backup.tar.gz /etc/myapp (c = create, z = gzip, f = file name). LIST: tar -tzf backup.tar.gz. EXTRACT: tar -xzf backup.tar.gz -C /tmp/restore. Modern tar detects compression on extract, so tar -xf works for .gz, .xz, and .zst.

  2. 02

    Compression choices: gzip (-z) is universal and fast; xz (-J) is smaller but slow; zstd (--zstd) is both fast and small, and increasingly the default for packages and backups. zip/unzip are common when exchanging files with Windows users.

  3. 03

    Useful extras: --exclude='*.log', -p to preserve permissions (default for root), streaming over SSH without a temporary file: tar -czf - /data | ssh backup 'cat > data.tar.gz'. gzip -k file keeps the original; zcat/zgrep/zless read compressed logs without extracting them.

  4. 04

    Always VERIFY backups: list or test-extract the archive and compare checksums (sha256sum). An archive nobody can extract is not a backup (Stateful Systems course, backups).

Code & diagrams

the commands you'll actually usebash
tar -czf app-config-$(date +%F).tar.gz /etc/myapp          # create (gzip)
tar -tzf app-config-2026-09-28.tar.gz | head                 # list contents
tar -xzf app-config-2026-09-28.tar.gz -C /tmp/restore        # extract elsewhere
tar --zstd -cf logs.tar.zst /var/log/myapp --exclude='*.tmp' # fast + small
zgrep -i "error" /var/log/nginx/access.log.2.gz | head       # search compressed logs
sha256sum app-config-2026-09-28.tar.gz > app-config.sha256   # checksum for verification

Explain it without notes

01

What's the difference between tar and gzip?

Practice

01

Copy a 20 GB directory to another server compressed, without writing a temporary archive to the local disk (which is nearly full).

Done when you can

  • I can create, list, and extract tar archives with gzip or zstd

  • I verify archives before trusting them as backups