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.
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
- 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, sotar -xfworks for .gz, .xz, and .zst. - 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/unzipare common when exchanging files with Windows users. - 03
Useful extras:
--exclude='*.log',-pto preserve permissions (default for root), streaming over SSH without a temporary file:tar -czf - /data | ssh backup 'cat > data.tar.gz'.gzip -k filekeeps the original;zcat/zgrep/zlessread compressed logs without extracting them. - 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
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 verificationExplain it without notes
What's the difference between tar and gzip?
Practice
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