bzip2Compress files with bzip2 for a better ratio
bzip2 compresses single files like gzip but usually smaller, at the cost of being several times slower to compress and to decompress. You'll meet it as .bz2 and .tar.bz2 files; bzcat and bzgrep read them in place.
bzip2 [OPTION]... [FILE]... | bunzip2 FILE.bz2 | bzcat FILE.bz2You archive a month of logs to cheap storage and want to know whether bzip2 is worth it over gzip.
bzip2Block-sorting compressor: Burrows-Wheeler transform plus Huffman coding.
bzip2 cuts the input into blocks of 100-900 KB and compresses each independently. In each block the Burrows-Wheeler transform sorts all rotations of the data, which groups similar contexts together so the output has long runs of the same character; a move-to-front step and run-length encoding turn those runs into small numbers, and Huffman coding packs them. Sorting is expensive, which is why bzip2 is slow in both directions, but it captures more structure than gzip's 32 KiB window. Each block carries its own CRC, and because blocks are independent, damaged files can be partly recovered with bzip2recover and the work can be parallelised: pbzip2 and lbzip2 use all cores and produce normal .bz2 files.
-kKeep the original file-dDecompress (same as bunzip2)-cWrite to stdout-1 ... -9Block size 100k ... 900k (default -9)-tTest integrity-vVerbose statistics-fOverwrite existing output filesbzcat / bzgrep / bzlessRead and search .bz2 files without extractingtar -cjfCreate a .tar.bz2 archiveUsing bzip2 for data that is read back oftenDecompression is about 5x slower than gzip and xz on the same data.
gzip or zstd for hot data; bzip2 or xz for write-once archivesbzip2 dir/ to compress a folderLike gzip, bzip2 only compresses single files.
tar -cjf dir.tar.bz2 dirAssuming bzip2 always beats xzIt depends on the data. xz usually wins on binaries and mixed data; bzip2 can win on repetitive text, as here.
Test on a real sample: ls -l after bzip2 -k and xz -kTry these on a Linux machine of your own. Each task has a hint and a solution.
Compress a copy of a file in ~/logs with bzip2, keeping the original, and print the statistics.
Show the last 3 lines of the .bz2 file without decompressing it to disk.
Create a bzip2-compressed tar archive of ~/data and list it.
bzip2Compress files with bzip2 for a better ratio
bzip2 compresses single files like gzip but usually smaller, at the cost of being several times slower to compress and to decompress. You'll meet it as .bz2 and .tar.bz2 files; bzcat and bzgrep read them in place.
bzip2 [OPTION]... [FILE]... | bunzip2 FILE.bz2 | bzcat FILE.bz2You archive a month of logs to cheap storage and want to know whether bzip2 is worth it over gzip.
bzip2Block-sorting compressor: Burrows-Wheeler transform plus Huffman coding.
bzip2 cuts the input into blocks of 100-900 KB and compresses each independently. In each block the Burrows-Wheeler transform sorts all rotations of the data, which groups similar contexts together so the output has long runs of the same character; a move-to-front step and run-length encoding turn those runs into small numbers, and Huffman coding packs them. Sorting is expensive, which is why bzip2 is slow in both directions, but it captures more structure than gzip's 32 KiB window. Each block carries its own CRC, and because blocks are independent, damaged files can be partly recovered with bzip2recover and the work can be parallelised: pbzip2 and lbzip2 use all cores and produce normal .bz2 files.
-kKeep the original file-dDecompress (same as bunzip2)-cWrite to stdout-1 ... -9Block size 100k ... 900k (default -9)-tTest integrity-vVerbose statistics-fOverwrite existing output filesbzcat / bzgrep / bzlessRead and search .bz2 files without extractingtar -cjfCreate a .tar.bz2 archiveUsing bzip2 for data that is read back oftenDecompression is about 5x slower than gzip and xz on the same data.
gzip or zstd for hot data; bzip2 or xz for write-once archivesbzip2 dir/ to compress a folderLike gzip, bzip2 only compresses single files.
tar -cjf dir.tar.bz2 dirAssuming bzip2 always beats xzIt depends on the data. xz usually wins on binaries and mixed data; bzip2 can win on repetitive text, as here.
Test on a real sample: ls -l after bzip2 -k and xz -kTry these on a Linux machine of your own. Each task has a hint and a solution.
Compress a copy of a file in ~/logs with bzip2, keeping the original, and print the statistics.
Show the last 3 lines of the .bz2 file without decompressing it to disk.
Create a bzip2-compressed tar archive of ~/data and list it.