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

bzip2 Command Guide for the Older Block Compressor

bzip2 compresses files using the Burrows–Wheeler block-sorting algorithm, producing .bz2 files that are smaller than gzip but larger than xz — the middle child of the compression family.


What is bzip2, and Why Use It?

bzip2 was the high-compression answer before xz existed. It sorts file content into blocks before compressing them, which beats plain DEFLATE (gzip) on text and data files. Today xz out-compresses it and gzip beats it on speed, so bzip2 survives mostly in older .tar.bz2 archives you may still download. You will rarely choose it on purpose, but you will recognize it: tar -j and .bz2 extensions belong to this tool.

Why Learn bzip2?

  • You will meet .tar.bz2 files — older releases and some Linux distributions still ship them.
  • tar -j uses it — knowing what the flag does completes the compression picture.
  • It sits between gzip and xz — understanding the trade-off curve explains why each format exists.
  • bzcat and bzmore read compressed files — same inspection trick as zcat.

bzip2 is rarely the right new choice — gzip for speed, xz for size. Learn it mainly so old .tar.bz2 files stop being mysterious.


1. Compress a File

bzip2 notes.txt

This creates notes.txt.bz2 and removes notes.txt. To keep the original:

bzip2 -k notes.txt
  • -k — keep the input file.

Choose a compression level from 1 (fastest) to 9 (smallest):

bzip2 -9 notes.txt

The default is -9, which is why it is slower than gzip.

2. Decompress a File

bunzip2 notes.txt.bz2
bzip2 -d notes.txt.bz2

Both restore notes.txt and remove the .bz2.

3. Compress Without Deleting

bzip2 -c notes.txt > notes-backup.bz2
  • -c — write to standard output instead of replacing the file.

4. Read a Compressed File Without Decompressing

bzcat notes.txt.bz2 | head -5
bzcat access.log.bz2 | grep "error"

bzcat prints the decompressed contents without touching the file — the same inspection trick zcat gives gzip files.

5. Compress Many Files

bzip2 *.log

Each file becomes its own .bz2. Verbosely:

bzip2 -kv *.log

6. Test Integrity

bzip2 -t backup.tar.bz2
  • -t — test the compressed data for corruption.

No output means the archive is fine.

7. The Relationship With tar

bzip2 and tar combine with the -j flag:

tar -cjf project.tar.bz2 project/
tar -xjf project.tar.bz2
  • -j — filter the archive through bzip2.

This is the same pattern as -z (gzip) and -J (xz) — one letter per compressor, same tar mechanics.


Common Pitfalls

  • Losing the original — bzip2 file deletes the input unless you pass -k.
  • Picking bzip2 for new archives — xz is smaller and often faster on modern hardware; choose gzip or xz unless a recipient needs .bz2.
  • Forgetting -j in tar — modern tar auto-detects compression, but -xjf is explicit and always works.
  • Assuming it beats xz — it does not; .tar.bz2 is usually larger than the equivalent .tar.xz.
  • Confusing bzip2 with bunzip2 on multi-file archives — bunzip2 expects .bz2 names; a .tar.bz2 must go through tar -xjf, not bunzip2 directly.
PageRelationship
tarThe bundler -j calls; the two produce .tar.bz2 together
gzipThe faster default compressor; bzip2 trades speed for ratio
xzThe modern successor; smaller files at similar effort
zipThe cross-platform archive format, unrelated to bzip2

bzip2 Cheatsheet

CommandWhat it does
bzip2 fileCompress to file.bz2, removing the original
bzip2 -k fileCompress but keep the original
bzip2 -c file > out.bz2Compress to standard output
bzip2 -9 fileCompress with the strongest setting
bunzip2 file.bz2Decompress back to the original file
bzip2 -d file.bz2Same as bunzip2
bzcat file.bz2 | lessRead a compressed file without decompressing
bzip2 -t file.bz2Test archive integrity
tar -cjf archive.tar.bz2 dir/Bundle and compress a directory with bzip2