Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 -r copies 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.gz or .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 — .zip opens natively on Windows, macOS, and Linux, which is why it survives as the “send it to anyone” format.

A good mental model: tar is 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

ExtensionWhat it isTypical use
.tarPlain archive, no compressionBundling only; rare on its own
.tar.gz / .tgztar + gzip compressionThe most common format on Linux
.tar.xztar + xz compressionSmallest size; slower to create
.tar.bz2tar + bzip2 compressionOlder format, still seen in the wild
.zipArchive + per-file compressionCross-platform sharing with Windows/macOS
.gz, .bz2, .xzA single compressed fileCompressing one file (logs, downloads)

Tools in This Section

PageWhat it doesReach for it when
tarCreate, extract, and list archives (with optional compression)You need to pack or unpack a directory — the default Linux choice
gzipCompress or decompress a single file (.gz)You want to shrink one file, or open a .gz download
zipCreate and extract .zip archivesYou need a file that Windows and macOS users can open easily
xzCompress a single file to the smallest size (.xz)Size matters more than speed
bzip2Compress 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 for tar -czf / tar -xzf. See .zip? Reach for zip / 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.