bzip2 Command in Linux with Examples

The bzip2 command is a powerful tool available in the Linux command line. It is primarily used for file compression and decompression, making it an essential utility for conserving storage space and managing file sizes. Furthermore, it offers different levels of compression, allowing users to choose between compression speed and file size.

How to Use the bzip2 Command: Syntax and Options

The bzip2 command’s syntax is fairly straightforward. Here it is in its most basic form:

bzip2 [options] [file ...]
  • bzip2: This is the command name.
  • [options]: These are command options that modify the behavior of the command.
  • [file ...]: This is the input file (or files) on which the command will operate.

The most commonly used options with the bzip2 command are:

  • -d or --decompress: Decompresses the specified file or files.
  • -k or --keep: Keeps (i.e., doesn’t delete) input files during compression or decompression.
  • -f or --force: Forces overwriting of output files.
  • -t or --test: Checks the integrity of the specified compressed file or files.
  • -v or --verbose: Displays detailed output about the command’s operation.
  • -q or --quiet: Suppresses non-essential warning messages and other output.
  • -1 .. -9: Specifies the block size to be used for compression, affecting the compression speed and resulting file size. A larger block size means higher compression, but it also means slower compression speed.

How to Use bzip2 Command in Linux: Practical Examples

Let’s now look at various examples illustrating the usage of the bzip2 command.

Compressing a Single File with bzip2

If you have a file that you want to compress, use the following command:

bzip2 filename

This will compress the specified file and replace it with a compressed version with a .bz2 extension.

Compressing Multiple Files with bzip2

To compress multiple files, you can use the bzip2 command as follows:

bzip2 filename1 filename2 filename3

This will compress each specified file individually, replacing each one with a compressed version with a .bz2 extension.

Decompressing a File with bzip2

You can decompress a .bz2 file using the -d option:

bzip2 -d filename.bz2

This will decompress the specified .bz2 file and replace it with the original uncompressed file.

Force Compression of a File Using bzip2

If a compressed version of a file already exists and you want to forcefully compress it again, use the -f option:

bzip2 -f filename

This will forcefully compress the file, overwriting any existing .bz2 file with the same name.

Preserving Original Files After Using bzip2

To keep the original files after compression or decompression, use the -k option:

bzip2 -k filename

This will compress the file and keep the original uncompressed file.

Verifying the Integrity of a Compressed File with bzip2

The -t option allows you to check the integrity of a .bz2 file:

bzip2 -t filename.bz2

This command tests the integrity of the compressed file, producing no output if the file is okay, and an error message if the file is corrupt.

Overwriting Output Files Using the bzip2 Command

To overwrite existing files without a confirmation prompt, use the -f option:

bzip2 -f filename

This will overwrite an existing .bz2 file with the same name without asking for confirmation.

Compressing or Decompressing Files to Standard Output with bzip2

The -c or --stdout option allows you to write the compressed or decompressed data to standard output (usually the terminal):

bzip2 -c filename > filename.bz2

This command will compress the file and write the compressed data to a new .bz2 file.

Adjusting Compression Level with bzip2

You can choose a compression level (from 1 to 9) with the -N option (N is a number from 1 to 9):

bzip2 -9 filename

The -9 option will provide the smallest file (slowest compression speed), while -1 gives the largest file (fastest compression speed).

Showing Detailed Output of bzip2 Command

To get a detailed output for your compression or decompression process, use the -v option:

bzip2 -v filename

This will display detailed information about the file’s compression or decompression, including the compression ratio and the saved or added space.

Suppressing Warning Messages with bzip2 Command

The -q option can be used to suppress warning messages and other non-essential output:

bzip2 -q filename

Reducing Memory Usage with bzip2 Command

You can reduce the amount of memory used by bzip2 with the -s or --small option:

bzip2 -s filename


The bzip2 command is a versatile and powerful tool for managing file compression and decompression on Linux systems. With the above examples and explanations, you should now be equipped to utilize this command effectively. Remember, efficient use of storage is vital in system administration, and tools like bzip2 are key to achieving this efficiency.