learninfra · Linux · Networking · Kubernetes · System Design · AI Infrastructure · Exam blueprints · Drills

Archives & Compression

Bundle a directory with tar, squeeze it with gzip, xz or zstd, and get it back intact: what each tool really saves, what it costs, and the ways an archive bites when you extract it.

An interactive Linux lesson: 22 steps, about 30 minutes, on a live simulation in your browser.

web01 is being replaced next month, and the shop's files in /srv/shop have to travel: configuration, the public pages and the customers' uploads. Copying a directory tree file by file is slow and loses track of who owned what. You want one file.

tar (tape archive) makes it. c creates, v lists each name as it goes in, f names the archive file. The first attempt, as alice, ends with Cannot open: Permission denied and exit status 2: config is readable only by root and the www-data group, so tar skipped it and wrote an archive without it. A backup that is missing files still looks like a backup.

What you will learn

  1. Many files, one file

    • Bundle the shop into one file: tar writes each file behind a 512-byte header that holds its name, mode, owner and time. It bundles; it does not compress.
    • Read an archive before you trust it: tar tvf is free: it shows every name, owner and mode in an archive and changes nothing on disk.
  2. What compression buys

    • 200 MB of log, two seconds of gzip
    • Does -9 buy you much?: Compression levels trade CPU for bytes with steep diminishing returns: the default level gets almost all of what the maximum gets.
    • bzip2, xz and zstd on the same log: gzip runs everywhere, zstd is fast and modern, xz is for when bytes matter more than minutes. Pick by who reads the file and how often you write it.
    • Compress a photo: Compression removes repetition. JPEG, MP4, .gz and encrypted files have none left, so compressing them again only adds a header.
  3. tar and a compressor together

    • One command: tar czf: A .tar.gz is a tar inside a gzip stream. Write it with tar czf; read it with tar tf or tar xf, which detect the compression themselves.
    • Break it: a .gz that is not gzip
  4. Extracting without surprises

    • A hotfix lands in your home directory: Extraction writes wherever the member names point, relative to your current directory, and overwrites without asking.
    • List, then extract into an empty directory: The safe habit: tar tf to read the names, mkdir an empty directory, tar xf -C into it. Add -k when existing files must survive.
    • The leading / is removed: tar stores paths without the leading /. Where the files land is decided at extraction: -C picks the root, --strip-components drops leading directories.
    • Drill: unpack a release, flat
  5. Did it arrive intact?

    • A fingerprint to send with the file: A checksum is a file's fingerprint: publish it next to the file, check it after every copy, and one changed byte changes the whole thing.
    • Break it: the copy that stopped short
    • Too big for the portal: split and cat: split cuts bytes. cat in the right order rebuilds the exact file, and the checksum proves it.
  6. zip, and what it forgets

    • zip versus tar on a Linux server: zip keeps names and rwx bits. tar also keeps owners, groups and the special bits. For files that land on a Linux server, use tar.
  7. A backup you can restore

    • The nightly backup, as a script
    • Restore one directory, owners and all: Name a member after the archive to extract only that part. Restore as root and tar puts owners and modes back as they were.
    • Which compressor, for what
    • Drill: a config backup, no leftovers
  8. Recap & playground

    • Cheat sheet
    • Playground