How to Use 'bzip2' to Compress Files

Use 'bzip2' instead of 'zip' when the size of the archive matters

Illustration of a magnifying glass over folders on a computer screen representing a Computer File Search

 alexsl / Getty Images

Linux, like Windows, supports several different algorithms for compressing groups of files into a single archive. The most common, the ZIP format, is nearly universal, but each compression algorithm offers relative strengths and weaknesses within the archive ecosystem.

Why Use 'bzip2' Over 'gzip'?

The gzip command uses the LZ77 compression method. The bzip2 compression tool uses the Burrows-Wheeler algorithm.

In a test, each command executed using the default compression settings and the compressed files were compared. The results show that the bzip2 command comes out on top for reducing the file size. However, it takes much longer to do so using bzip2.

The lzmash command is the equivalent of running gzip with the compression level set to -9—its highest compression setting. Lzmash takes longer than gzip by default but the resulting archive file is smaller than the bzip2 equivalent.

Compressing Files Using 'bzip2'

To compress a file using the bzip2 format, run the following command:

bzip2 filename

The resulting archive file uses the .bz2 extension. This utility compress the file even if the file becomes larger as a result. This oddity occurs when you compress a file that has already been compressed.

How to Decompress Files

Decompress files with the bz2 extension with the bunzip2 command:

bunzip2 filename.bz2

The bunzip2 command extracts any valid file that has been compressed using bzip or bzip2. As well as decompressing ordinary files, it can also decompress tar files, which have been compressed using the bzip2 command.

Tar files compressed using the bzip2 command use extension .tbz2. When you decompress this file using the bunzip2 command the filename becomes filename.tar.

Command Options

Modify how bzip2 works through command options:

  • -f: Force compression even if the file is already in BZ2 format.
  • -k: Keep the original file as well as the compressed archive.
  • -s: Reduce the system memory allocated to the compression job, thereby extended the time it takes for the compression to complete.
  • -t: Test the archive file.
  • -v: Report verbose output about the command's execution.

Use the separate bzip2recover utility to recover a corrupt archive. The utility won't always work, but it's better than nothing if your archive cannot be decompressed.