Read

Module 8 · Archives, compression and the network

Other compressors: bzip2, xz and unzip

Recognise .bz2, .xz and .zip files by their extension and open each one with the right tool: bunzip2 and bzcat, unxz and xzcat, unzip.

What you will learn

  • Match .gz, .bz2, .xz and .zip files to the tool that opens them.
  • Decompress and read .bz2 and .xz files, and unpack .tar.xz with a pipe.
  • List and extract ZIP archives with unzip, `-l`, `-d`, `-o` and `-p`.

gzip is the most common compressor on Linux, but not the only one. Software releases are often published as .tar.xz because xz squeezes harder; older archives use .tar.bz2; and files that come from Windows users arrive as .zip. The family resemblance is strong: the extension tells you the tool, and each tool has the same three verbs you learned with gzip: decompress, keep, write to stdout.

ExtensionDecompressRead to stdoutCompress (full distros)
.gzgunzipzcatgzip
.bz2bunzip2bzcatbzip2
.xzunxz, xz -dxzcatxz
.zipunzipunzip -pzip

bzip2 and xz

Both behave exactly like gunzip. bunzip2 notes.txt.bz2 replaces the file with notes.txt; -k keeps the compressed copy; -t tests it; bzcat prints it. For xz, unxz and xz -d are the same command, and xzcat prints. And because this tar cannot decompress by itself, a .tar.xz or .tar.bz2 is opened with the same pipe as a .tar.gz, swapping the cat:

~% bzcat notes.txt.bz2
bzip2 works: these notes were compressed with bzip2.
~% bunzip2 -k notes.txt.bz2
~% ls
notes.txt  notes.txt.bz2  photos.zip  release.tar.xz
~% xzcat release.tar.xz | tar tvf -
-rw-r--r-- 0/0         6 2026-09-21 14:13:20 release/VERSION
-rw-r--r-- 0/0        27 2026-09-21 14:13:20 release/CHANGELOG
~% xzcat release.tar.xz | tar xf -
~% cat release/VERSION
2.4.1

Why so many formats? They trade speed for size. gzip is fast and good enough; bzip2 is slower and a little smaller; xz is the slowest to compress but makes the smallest files and still decompresses quickly, which is why distributions use it for packages that are built once and downloaded millions of times.

unzip

ZIP is different: it is an archive and a compressor in one, so there is no tar step. unzip -l file.zip lists the contents with sizes and dates. unzip file.zip extracts into the current directory. -d DIR extracts somewhere else, and in this BusyBox version the directory must exist first. If a file already exists, unzip asks what to do; -o overwrites without asking and -n never overwrites, which is what you want in scripts. -p prints a member to stdout, like tar xO.

~% unzip -l photos.zip
Archive:  photos.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       21  07-01-2026 12:00   photos/readme.txt
       32  07-01-2026 12:00   photos/list.txt
~% mkdir -p out && unzip -d out photos.zip
Archive:  photos.zip
  inflating: photos/readme.txt
  inflating: photos/list.txt
~% unzip -p photos.zip photos/list.txt
beach.jpg
mountain.jpg
city.jpg

Commands in this lesson

CommandWhat it does
bunzip2 file.bz2Decompress a .bz2 (-k keeps it).
bzcat file.bz2Print a .bz2's contents.
unxz file.xzDecompress a .xz (same as xz -d).
xzcat a.tar.xz | tar xf -Unpack a .tar.xz.
unzip -l a.zipList a ZIP archive.
unzip -d dir a.zipExtract into an existing dir.
unzip -o a.zipExtract, overwriting without asking.
unzip -p a.zip memberPrint one member to stdout.

Quiz

  1. Which command prints the contents of log.bz2 without creating any file?

    • zcat log.bz2
    • bzcat log.bz2
    • bunzip2 log.bz2
    • cat log.bz2
  2. How do you unpack app-2.4.tar.xz in this VM?

    • unzip app-2.4.tar.xz
    • xzcat app-2.4.tar.xz | tar xf -
    • tar xf app-2.4.tar.xz
    • gunzip app-2.4.tar.xz
  3. Why is there no tar step when you open a .zip?

    • ZIP is both the archive and the compression
    • ZIP files are never compressed
    • unzip calls tar internally
    • There is: unzip then tar xf
  4. A script runs `unzip data.zip` and hangs when the files already exist. What fixes it?

    • unzip -l data.zip
    • unzip -o data.zip (or -n)
    • unzip -p data.zip
    • Running it twice
  5. Which format usually gives the smallest file, at the cost of slow compression?

    • gzip
    • xz
    • tar
    • zip

Practice

  1. Decompress `/root/lab/l74/notes.txt.bz2` so that `/root/lab/l74/notes.txt` exists.

  2. Unpack `/root/lab/l74/release.tar.xz` inside `/root/lab/l74`, so that `/root/lab/l74/release/VERSION` exists.

  3. Extract `/root/lab/l74/photos.zip` into the directory `/root/lab/l74/unpacked`, so that `/root/lab/l74/unpacked/photos/readme.txt` exists.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.