CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Archiving & Compression / bzip2

bzip2

Compress files with bzip2 for a better ratio

Beginnercompressionarchive

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.

Syntaxbzip2 [OPTION]... [FILE]... | bunzip2 FILE.bz2 | bzcat FILE.bz2

Watch it run

You archive a month of logs to cheap storage and want to know whether bzip2 is worth it over gzip.

devops@devops-lab: ~/logs — zsh — 96×28
➜ ~/logs
Step 1 of 3
time bzip2 -kv access.log

-v prints the ratio (15.9 to 1), bits needed per input byte and the saving. It took 17 seconds, against 2.5 for gzip.

Anatomy of a command

$
commandbzip2

Block-sorting compressor: Burrows-Wheeler transform plus Huffman coding.

How it works

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.

Options you'll actually use

-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 files
bzcat / bzgrep / bzlessRead and search .bz2 files without extracting
tar -cjfCreate a .tar.bz2 archive

Common mistakes

Using bzip2 for data that is read back often

Decompression is about 5x slower than gzip and xz on the same data.

gzip or zstd for hot data; bzip2 or xz for write-once archives
bzip2 dir/ to compress a folder

Like gzip, bzip2 only compresses single files.

tar -cjf dir.tar.bz2 dir
Assuming bzip2 always beats xz

It 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 -k

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Compress a copy of a file in ~/logs with bzip2, keeping the original, and print the statistics.

  2. 2

    Show the last 3 lines of the .bz2 file without decompressing it to disk.

  3. 3

    Create a bzip2-compressed tar archive of ~/data and list it.

Related commands

gzipCompress and decompress single files with gzipxzCompress files to the smallest size with xztarBundle files into an archive and extract them
PreviousgzipNextxz
Linux / Archiving & Compression / bzip2

bzip2

Compress files with bzip2 for a better ratio

Beginnercompressionarchive

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.

Syntaxbzip2 [OPTION]... [FILE]... | bunzip2 FILE.bz2 | bzcat FILE.bz2

Watch it run

You archive a month of logs to cheap storage and want to know whether bzip2 is worth it over gzip.

devops@devops-lab: ~/logs — zsh — 96×28
➜ ~/logs
Step 1 of 3
time bzip2 -kv access.log

-v prints the ratio (15.9 to 1), bits needed per input byte and the saving. It took 17 seconds, against 2.5 for gzip.

Anatomy of a command

$
commandbzip2

Block-sorting compressor: Burrows-Wheeler transform plus Huffman coding.

How it works

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.

Options you'll actually use

-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 files
bzcat / bzgrep / bzlessRead and search .bz2 files without extracting
tar -cjfCreate a .tar.bz2 archive

Common mistakes

Using bzip2 for data that is read back often

Decompression is about 5x slower than gzip and xz on the same data.

gzip or zstd for hot data; bzip2 or xz for write-once archives
bzip2 dir/ to compress a folder

Like gzip, bzip2 only compresses single files.

tar -cjf dir.tar.bz2 dir
Assuming bzip2 always beats xz

It 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 -k

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Compress a copy of a file in ~/logs with bzip2, keeping the original, and print the statistics.

  2. 2

    Show the last 3 lines of the .bz2 file without decompressing it to disk.

  3. 3

    Create a bzip2-compressed tar archive of ~/data and list it.

Related commands

gzipCompress and decompress single files with gzipxzCompress files to the smallest size with xztarBundle files into an archive and extract them
PreviousgzipNextxz