bzip2 — BWT compression better-ratio than gzip, .bz2 single-file, lost ground to xz/zstd modern alternatives across all 5 shells
Equivalents in every shell
bzip2 -k file.txt`bzip2 FILE` compresses FILE → `FILE.bz2` and REMOVES the original (same destructive-default as gzip). `-k` keep original. `-d` decompress. `-c` write to stdout. `-1` through `-9` block-size 100KB through 900KB (default `-9`). `bunzip2` is a symlink to `bzip2 -d`. `bzcat`/`bzless`/`bzgrep` operate on .bz2 WITHOUT extracting. Installed by default on most distros (Ubuntu, Debian, Fedora, Arch) — the LZMA `xz` is faster but bzip2 remains common for legacy compatibility.
bzip2 -k file.txtbzip2 -k file.txt# pwsh has no bzip2 cmdlet. Install via `winget install GnuWin32.bzip2` or use 7-Zip: & "$env:ProgramFiles\7-Zip\7z.exe" a -tbzip2 file.bz2 file.txtNo native pwsh / .NET bzip2 support — `System.IO.Compression` has Deflate (gzip) and Brotli but not bzip2. Workarounds: 7-Zip command-line (`7z a -tbzip2 ...`), GnuWin32 `bzip2.exe`, or NuGet `SharpZipLib` package: `Add-Type -Path SharpZipLib.dll; ICSharpCode.SharpZipLib.BZip2.BZip2.Compress(...)`.
powershell -NoProfile -Command "& '$env:ProgramFiles\7-Zip\7z.exe' a -tbzip2 file.bz2 file.txt"cmd has no bzip2. After `winget install 7zip.7zip`: `7z a -tbzip2 file.bz2 file.txt` creates the .bz2. Or install GnuWin32 bzip2 (single binary `bzip2.exe`). WSL is the friction-free path on Windows.
Worked examples
Compress a file (keep original)
bzip2 -k file.txtbzip2 -k file.txt& "$env:ProgramFiles\7-Zip\7z.exe" a -tbzip2 file.bz2 file.txt7z a -tbzip2 file.bz2 file.txtDecompress a .bz2 file
bunzip2 file.txt.bz2bunzip2 file.txt.bz2& "$env:ProgramFiles\7-Zip\7z.exe" x file.txt.bz27z x file.txt.bz2View .bz2 contents without extracting
bzcat file.txt.bz2bzcat file.txt.bz2& "$env:ProgramFiles\7-Zip\7z.exe" x -so file.txt.bz27z x -so file.txt.bz2Gotchas
- **`bzip2` REMOVES the source by default — same as `gzip`**. `bzip2 file.txt` produces `file.txt.bz2` and DELETES `file.txt`. `-k` (keep) preserves the original. This trap dates to 1996 when disk space was scarce; for modern usage where you almost always want to keep the source: alias `bzip2="bzip2 -k"` in your shell rc. Same applies to `bunzip2` — `bunzip2 file.txt.bz2` produces `file.txt` and DELETES the .bz2.
- **bzip2 LOST GROUND to xz and zstd in the 2010s**. The compression ratio sweet spot: bzip2 is ~10% better than gzip but 2-3x slower; xz is ~30% better than gzip but 5-10x slower at compress (similar decompress speed to gzip); zstd is gzip-speed with bzip2-level compression. Modern Linux source tarballs use .tar.xz (kernel.org). Modern containers + distros (Facebook + Debian + Arch + Fedora 2026) default to .tar.zst. bzip2 still appears in: (a) legacy .tar.bz2 archives, (b) pacman package format until ~2019, (c) embedded systems where xz/zstd aren't available. For NEW pipelines: prefer zstd (`zstd -3` for fastest, `zstd -19` for max).
- **Single-threaded — `pbzip2` is the parallel fork**. Plain `bzip2` uses ONE CPU core, regardless of how many you have. On modern multi-core systems this is glacial — compressing a 10 GB log can take 30+ minutes single-threaded. `pbzip2 file.txt` uses all cores in parallel — same compression ratio, near-linear speedup. Different output format (uses multiple bzip2 streams concatenated) — `bunzip2` decompresses pbzip2 archives correctly. Same lineage applies to gzip (`pigz`) and xz (`xz -T0` built-in multithread, no separate binary needed).
- **BWT (Burrows-Wheeler Transform) is memory-intensive** — block-size `-9` (the default) needs ~7900 KB of memory per concurrent compression. On embedded / IoT devices, drop to `-1` (~1300 KB block memory). The block size is also the recovery granularity — if a .bz2 file is partially corrupted, you can recover the unaffected blocks: `bzip2recover file.bz2` splits the archive into individual blocks (file.bz2.rec000{1,2,3}.bz2), then `bzcat *.rec*.bz2 > recovered.txt`. xz and zstd have NO equivalent recovery tool — bzip2's block structure is uniquely suited for partial-recovery.
- macOS has `bzip2` in `/usr/bin/bzip2` (BSD-licensed reimplementation by Julian Seward, same as Linux) — works identically to Linux. The flag set is fully cross-platform compatible. `tar cjf` (the `j` for bzip2) works on both: `tar cjf archive.tar.bz2 dir/`. Note: modern `tar` AUTO-DETECTS compression from the extension — `tar xf file.tar.bz2` works without explicit `j` on GNU tar 1.22+ (2009) and BSD tar.