bzip2¶
Format: .bz2 · Algorithm: Burrows–Wheeler transform + move-to-front + Huffman · First released: 1996 (Julian Seward)
bzip2 offered noticeably better ratios than gzip at the cost of being much slower, and through the 2000s .tar.bz2 was the format of choice for source tarballs. Today zstd and xz beat it on both ratio and speed, so it's rarely the right choice for new data. You'll still meet it in older archives and some large public data dumps.
Its predecessor, bzip (.bz), used arithmetic coding and was withdrawn over patent concerns.
Basics¶
bzip2 file # -> file.bz2, removes file
bzip2 -k file # keep original
bzip2 -d file.bz2 # decompress (same as bunzip2)
bunzip2 file.bz2
bunzip2 -k file.bz2
bzip2 -c file > out.bz2 # to stdout
bzip2 -t file.bz2 # test integrity
bzip2 -v file # verbose
bzip2 -9 file # 900 KB blocks (default)
bzip2 -1 file # 100 KB blocks
The -1 … -9 flag sets the block size (100–900 KB), not "effort". Bigger blocks mean a better ratio and more memory; the default is already the maximum.
Working with .bz2 files without extracting¶
bzcat file.bz2
bzless file.bz2
bzmore file.bz2
bzgrep 'pattern' file.bz2
bzdiff file1.bz2 file2.bz2
bzcmp file1.bz2 file2.bz2
bzdiffon two.tar.bz2files diffs the raw tar streams, not the files inside them. To compare the trees, extract both anddiff -r, or compare listings:
bash diff <(tar tjf a.tar.bz2 | sort) <(tar tjf b.tar.bz2 | sort)
With tar¶
tar cjvf archive.tar.bz2 src_dir/ # create
tar xjvf archive.tar.bz2 # extract (or just tar xf)
tar tjvf archive.tar.bz2 # list
tar -I lbzip2 -cf archive.tar.bz2 src_dir/ # parallel
See tar for the full set of options.
Recovery¶
bzip2 compresses independent blocks, so damage is usually confined to one block, and it ships with a recovery tool:
bzip2recover damaged.bz2 # writes rec00001damaged.bz2, rec00002damaged.bz2, ...
bzip2 -t rec*.bz2 # find the intact blocks
cat $(ls rec*.bz2) > partially-recovered.bz2
Parallel implementations¶
Plain bzip2 is single-threaded and slow in both directions. Two format-compatible parallel versions exist:
| Tool | Notes |
|---|---|
pbzip2 |
Parallel compression; parallel decompression only for files it created |
lbzip2 |
Parallel compression and decompression of any bzip2 file; usually the faster choice |
lbzip2 -n 8 file
lbzip2 -d file.bz2
pbzip2 -p8 file
See parallel compression.
When to still use it¶
- Something downstream only accepts
.bz2. - You're appending to an existing collection that's already in bzip2.
Otherwise, use zstd for speed or xz for ratio.
See also¶
- tar
- xz and zstd
- Cheat sheet