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.bz2files — older releases and some Linux distributions still ship them. tar -juses 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.
bzcatandbzmoreread compressed files — same inspection trick aszcat.
bzip2is rarely the right new choice —gzipfor speed,xzfor size. Learn it mainly so old.tar.bz2files 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 throughbzip2.
This is the same pattern as -z (gzip) and -J (xz) — one letter per compressor, same tar mechanics.
Common Pitfalls
- Losing the original —
bzip2 filedeletes the input unless you pass-k. - Picking bzip2 for new archives —
xzis smaller and often faster on modern hardware; choosegziporxzunless a recipient needs.bz2. - Forgetting
-jin tar — modern tar auto-detects compression, but-xjfis explicit and always works. - Assuming it beats xz — it does not;
.tar.bz2is usually larger than the equivalent.tar.xz. - Confusing
bzip2withbunzip2on multi-file archives —bunzip2expects.bz2names; a.tar.bz2must go throughtar -xjf, notbunzip2directly.
Related Pages
| Page | Relationship |
|---|---|
| tar | The bundler -j calls; the two produce .tar.bz2 together |
| gzip | The faster default compressor; bzip2 trades speed for ratio |
| xz | The modern successor; smaller files at similar effort |
| zip | The cross-platform archive format, unrelated to bzip2 |
bzip2 Cheatsheet
| Command | What it does |
|---|---|
bzip2 file | Compress to file.bz2, removing the original |
bzip2 -k file | Compress but keep the original |
bzip2 -c file > out.bz2 | Compress to standard output |
bzip2 -9 file | Compress with the strongest setting |
bunzip2 file.bz2 | Decompress back to the original file |
bzip2 -d file.bz2 | Same as bunzip2 |
bzcat file.bz2 | less | Read a compressed file without decompressing |
bzip2 -t file.bz2 | Test archive integrity |
tar -cjf archive.tar.bz2 dir/ | Bundle and compress a directory with bzip2 |