Archives & Compression
Every Linux command you have learned so far works on files as they sit on your disk. But sooner or later you need to move a whole directory to another machine, shrink a log file that is eating your disk, or unpack software someone gave you as a .tar.gz. That is the job of archives (bundling many files into one) and compression (shrinking that bundle). This section covers the five tools that do almost all of it on Linux: tar, gzip, zip, xz, and bzip2.
What Is an Archive, and Why Learn It?
An archive is a single file that contains other files and directories — like a box you pack your belongings into before a move. Most archives also compress their contents, shrinking them so they transfer faster and take up less space.
Why Learn Archives?
- Moving whole directories —
cp -rcopies files, but an archive packages a directory plus its permissions, symlinks, and empty subdirectories into one portable file. - Software distribution — most source code and Linux software arrives as
.tar.gzor.tar.xz; you cannot install it without unpacking it first. - Saving disk space — text compresses dramatically (often 3–5× smaller), which matters for logs, backups, and containers.
- Backups — a dated tarball (
backup-2026-12-05.tar.gz) is the simplest backup strategy there is. - Cross-platform sharing —
.zipopens natively on Windows, macOS, and Linux, which is why it survives as the “send it to anyone” format.
A good mental model:
taris the box — it packs many files into one and keeps their metadata. The compressors (gzip,xz,bzip2) are the vacuum bags — they shrink a single file. In practice you almost always do both at once: tar the directory, then compress the tarball.
Common Archive Formats
| Extension | What it is | Typical use |
|---|---|---|
.tar | Plain archive, no compression | Bundling only; rare on its own |
.tar.gz / .tgz | tar + gzip compression | The most common format on Linux |
.tar.xz | tar + xz compression | Smallest size; slower to create |
.tar.bz2 | tar + bzip2 compression | Older format, still seen in the wild |
.zip | Archive + per-file compression | Cross-platform sharing with Windows/macOS |
.gz, .bz2, .xz | A single compressed file | Compressing one file (logs, downloads) |
Tools in This Section
| Page | What it does | Reach for it when |
|---|---|---|
tar | Create, extract, and list archives (with optional compression) | You need to pack or unpack a directory — the default Linux choice |
gzip | Compress or decompress a single file (.gz) | You want to shrink one file, or open a .gz download |
zip | Create and extract .zip archives | You need a file that Windows and macOS users can open easily |
xz | Compress a single file to the smallest size (.xz) | Size matters more than speed |
bzip2 | Compress a single file, better ratio than gzip (.bz2) | You meet an older .bz2 file or need a middle ground |
The one tool to memorize first is tar: it both archives and drives the compressors, so it covers most real-world situations by itself.
A Worked Example: Back Up a Project
Suppose you want to back up a project directory, shrink it, and send it to a friend:
# 1. Pack the directory into a compressed tarball
tar -czf myproject-backup.tar.gz myproject/
# 2. Check what is inside without extracting anything
tar -tzf myproject-backup.tar.gz | head
# 3. See how much space was saved
ls -lh myproject-backup.tar.gz
du -sh myproject/
# 4. Later, restore it wherever you like
tar -xzf myproject-backup.tar.gz -C ~/restored/
One command packed everything, another listed it, another restored it — and the whole backup is a single file small enough to email.
Why This Matters
- You will meet archives constantly — every tutorial that says “download and extract” means one of the tools in this section.
- Compression is free disk space — gzip-level compression of text is nearly free to apply and saves 60–80% on logs.
- Knowing the format tells you the tool — see
.tar.gz? Reach fortar -czf/tar -xzf. See.zip? Reach forzip/unzip.
The next section, System Monitoring & Logs, covers free, uptime, journalctl, and friends — the tools for checking what your machine is doing right now.
Remember: when in doubt,
tar. It creates the archive, compresses it, extracts it, and lists it — and the flags (-czf,-xzf,-tzf) are the same in almost every Linux tutorial you will ever read.