bzip2
37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0bzip2, by Julian Seward, compresses with the Burrows–Wheeler transform in independent blocks of 100–900 kB. It beats gzip on ratio, particularly for text, but is slow in both directions, single-threaded, and is outperformed on speed, and usually on ratio, by xz and zstd. Today it is mainly something you decompress: old source tarballs (.tar.bz2) and some datasets. For new files, use zstd or xz.
Common commands
Section titled “Common commands”bzip2 -k file # file.bz2, 900k blocks, keep the originalbzip2 -1 file # 100k blocks: faster, less memory, worse ratiobzip2 -d file.bz2 # decompress (or bunzip2); bzcat to stdoutbzip2 -t file.bz2 # test integritybzip2 deletes the input unless you pass -k. The -1 … -9 options set the block size to 100 kB … 900 kB; the default is -9, and smaller blocks only help when memory is limited. Compression at -9 needs about 7.6 MB of memory and decompression about 3.7 MB (-s reduces it to at most 2.5 MB, at some cost in speed).
Archives and parallelism
Section titled “Archives and parallelism”tar -cjf backup.tar.bz2 dir/tar -xf backup.tar.bz2The reference bzip2 uses a single thread. Because blocks are independent, parallel implementations such as pbzip2 and lbzip2 can speed it up and also decompress .bz2 files in parallel. 7-Zip can also create bzip2 with multiple threads (7z a -tbzip2).
Pitfalls
Section titled “Pitfalls”- Block independence helps recovery. Damage to one block loses only that block, and
bzip2recovercan extract the intact ones. gzip has no equivalent. - Poor on binary or already-compressed data and slower than modern alternatives; the gain over gzip rarely justifies the time.
.bz2is a single stream, not an archive. Combine it with tar for directories.