Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Archives and Compression

Linux bzip2 command

Reference for the Linux bzip2 and bunzip2 commands: compress with the Burrows-Wheeler algorithm, keep originals with -k, set block size, test files and use pipes.

bzip2 compresses files with the Burrows-Wheeler transform plus Huffman coding. It usually produces smaller files than gzip on text and source code, but compresses and decompresses more slowly. bunzip2 decompresses, bzcat writes decompressed data to standard output.

Like gzip it works on a single stream and replaces name with name.bz2, so directories need tar (tar -cjf dir.tar.bz2 dir). Today xz and zstd have mostly replaced it for new uses, but you still meet .bz2 files in older archives and datasets.

Terminal
bzip2 report.txt        # makes report.txt.bz2
bunzip2 report.txt.bz2

Try it

Bash Compress and compare with gzip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Block size, streams and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
bzip2 [OPTION]... [FILE]...
bunzip2 [OPTION]... [FILE.bz2]...
bzcat [FILE.bz2]...
Option Description
-z, --compress Compress (the default).
-d, --decompress Decompress (same as bunzip2).
-k, --keep Keep input files.
-c, --stdout Write to standard output.
-1 ... -9 Block size 100 kB to 900 kB; default -9. Larger blocks compress better and use more memory.
-f, --force Overwrite existing output files.
-t, --test Test integrity.
-v, --verbose Report ratio per file.
-q, --quiet Suppress non-critical messages.

With no file names, bzip2 reads standard input and writes standard output.

Returns: 0 on success, 1 for environment problems (missing file, bad option), 2 for a corrupt compressed file, 3 for an internal error.

Usage notes

  • Memory. Compression needs about 8 times the block size, around 7 MB at the default -9; decompression needs about 4 times (3.7 MB). Small by modern standards.
  • Speed. Slower than gzip in both directions and single threaded; pbzip2 and lbzip2 give compatible parallel versions.
  • Recovery. Each block is independent, so a damaged file can sometimes be partly recovered with bzip2recover.
  • Not splittable by lines, but by blocks. Big-data tools can process the blocks of a .bz2 file in parallel, which is why it was popular on Hadoop.
  • Does not compress already compressed data, and tiny files may even grow by a few dozen bytes.
  • Replaces the input. Without -k or -c the original is removed on success.
  • Suffix handling. bunzip2 file.tbz2 and .tbz restore .tar; for unknown suffixes bunzip2 appends .out.
  • With tar: tar -xjf archive.tar.bz2 extracts; GNU tar also detects the format by itself with plain -x.
  • Choose by need. gzip for speed and compatibility, bzip2 for better ratio at moderate cost, xz for smallest output, zstd for fast with good ratios.