bzip2 Command in Linux: Complete Guide with Examples & Best Practices

bzip2 Command in Linux: Complete Guide with Examples & Best Practices

Tested on Ubuntu 22.04 / 24.04, Debian 12, and compatible Linux distributions

The bzip2 command in Linux is a widely used file compression utility that reduces file size using the Burrows–Wheeler algorithm and Huffman coding. Compared to gzip, bzip2 usually provides a better compression ratio, making it suitable when saving disk space is more important than speed.

Note that bzip2 can compress only files, not directories. To compress folders, it is commonly used together with the tar command.


bzip2 Command Syntax and Overview

bzip2 Command Syntax

text
bzip2 [options] filename

If no filename is provided, bzip2 reads from standard input and writes to standard output. By default, it replaces the original file with a .bz2 compressed version.

bzip2 Command – Quick Cheat Sheet

This quick reference summarizes the most commonly used bzip2 command examples in Linux for compressing and decompressing files efficiently.

TaskCommand
Compress a filebzip2 file.txt
Compress & keep originalbzip2 -k file.txt
Compress multiple filesbzip2 file1 file2 file3
Compress using wildcardbzip2 *.txt
Decompress a filebzip2 -d file.txt.bz2
Decompress (alternate)bunzip2 file.txt.bz2
Test compressed filebzip2 -t file.txt.bz2
Force overwritebzip2 -f file.txt
Fast compressionbzip2 -1 file.txt
Best compressionbzip2 -9 file.txt
Verbose outputbzip2 -v file.txt
Quiet modebzip2 -q file.txt
Output to stdoutbzip2 -c file.txt > file.txt.bz2
Pipe with tarbzip2 -dc archive.tar.bz2 | tar tf -
Low memory modebzip2 -s file.txt

Basic Compression and Decompression

Compress a File Using bzip2

This example compresses a single file and replaces it with a .bz2 file. It is the most basic and commonly used bzip2 operation.

text
bzip2 test.txt

bzip2 command to compress a file

After execution, test.txt is replaced by test.txt.bz2.

Decompress a File

Use the -d or --decompress option to extract a .bz2 file back to its original format.

text
bzip2 -d test.txt.bz2

The compressed file is replaced with the decompressed original file.

Decompress Files Using bunzip2

The bunzip2 command is functionally equivalent to bzip2 -d and is often preferred for readability when only decompression is required.

text
bunzip2 test.txt.bz2

This command restores test.txt from test.txt.bz2.


Working with Multiple Files

Compress Multiple Files

You can compress multiple files in a single command by listing them space-separated. Each file is compressed individually.

text
bzip2 file1.txt file2.txt file3.txt

Each input file is replaced by its corresponding .bz2 version.

Compress Files Using Wildcards

Wildcards allow you to compress multiple files of the same type in one go. This is useful when working with log files or text files in bulk.

text
bzip2 *.txt

All .txt files in the current directory are compressed.


Preserving and Overwriting Files

Keep Original Files After Compression or Decompression

By default, bzip2 removes the input file. The -k or --keep option preserves the original file.

text
bzip2 -k test.txt

Both test.txt and test.txt.bz2 will exist after the command completes.

Force Overwrite Existing Files

If the output file already exists, bzip2 refuses to overwrite it. Use the -f or --force option to override this behavior.

text
bzip2 -f test.txt

This option is useful in scripts where manual confirmation is not possible.

Handling .bz2 Files Without Removing Original

By default, bzip2 removes the original file after compression and removes the .bz2 file after decompression. To preserve the original file, use the -k (keep) option.

Compress a file and keep the original:

text
bzip2 -k file.txt

Result:

  • file.txt (original file is preserved)
  • file.txt.bz2 (compressed file)

Decompress a file without deleting the .bz2 file:

text
bzip2 -dk file.txt.bz2

Result:

  • file.txt.bz2 (compressed file is preserved)
  • file.txt (decompressed file)

To explicitly keep the original file during decompression, combine -d (decompress) with -k i.e. bzip2 -dk archive.bz2


Compression Control and Performance

Set Compression Level

bzip2 supports compression levels from -1 (fastest) to -9 (best compression). The default level is -6.

text
bzip2 -1 test.txt
bzip2 -9 test.txt

Higher levels reduce file size further but require more CPU time.

Reduce Memory Usage

The -s or --small option reduces memory consumption during compression, decompression, and testing. This is useful on low-memory systems.

text
bzip2 -s test.txt

This option trades performance for lower memory usage.

Parallel Compression Using pbzip2

pbzip2 is a parallel implementation of bzip2 that uses multiple CPU cores to significantly speed up compression and decompression.

Install pbzip2:

text
sudo apt install pbzip2

Compress a file using all available CPU cores:

text
pbzip2 file.txt

Decompress a file:

text
pbzip2 -d file.txt.bz2

Limit the number of CPU cores used:

text
pbzip2 -p4 largefile.log

Preserve the original file during compression:

text
pbzip2 -k file.txt

Note:

  • Output files remain fully compatible with standard bzip2
  • Best suited for files larger than a few hundred MB

When NOT to Use bzip2 (Performance Trade-offs)

While bzip2 provides better compression ratios than gzip, it is not always the best choice.

Avoid bzip2 when:

  • You need very fast compression or decompression
  • Working with small files (overhead is not worth it)
  • Running on low-memory or low-CPU systems
  • Real-time streaming performance is required

Comparison summary:

  • gzip → fastest compression and decompression
  • bzip2 → better compression, slower speed
  • xz → best compression, highest CPU and memory usage

Typical recommendations:

  • Use gzip for logs and frequent access
  • Use bzip2 for medium-term storage
  • Use xz for long-term archival where speed is less important

Enable Verbose Output

Verbose mode displays detailed information such as compression ratio and processing status.

text
bzip2 -v test.txt

bzip2 verbose output showing verbosity level

Adding multiple -v flags increases the verbosity level.


Validation and Integrity Checks

Test Integrity of a Compressed File

The -t or --test option verifies whether a .bz2 file is valid and uncorrupted without extracting it.

text
bzip2 -t test.txt.bz2

bzip2 command to test integrity of a compressed file

If the file is healthy, bzip2 reports an ok status.

Check Compressed File Size Without Decompression

You can check the size of a .bz2 file without decompressing it using standard file inspection commands. This is useful when verifying storage usage or estimating transfer size.

Check compressed file size using ls:

text
ls -lh archive.bz2

Check size using du (actual disk usage):

text
du -h archive.bz2

View detailed file information:

text
stat archive.bz2

Note:

  • These commands show the compressed size only
  • They do not reveal the original (uncompressed) size

To see both compressed and uncompressed sizes, use bzip2 -tv archive.bz2


Streams and Pipelines

Compress or Decompress to Standard Output

The -c or --stdout option sends output to standard output instead of creating or replacing files. This is commonly used with pipes.

text
bzip2 -c test.txt > test.txt.bz2
bzip2 -dc archive.tar.bz2 | tar tf -

This approach is useful for chaining bzip2 with other commands.

Compress a Directory Using tar and bzip2

bzip2 works on individual files. To compress an entire directory, combine it with tar.

Create a .tar.bz2 archive:

text
tar -cjf backup.tar.bz2 mydir/

Explanation:

  • -c → create archive
  • -j → use bzip2 compression
  • -f → specify output file

Preserve permissions and ownership (recommended for backups):

text
tar -cjpSf backup.tar.bz2 mydir/

Extract tar.bz2 Archives

Extract a .tar.bz2 archive using tar.

Extract archive contents:

text
tar -xjf backup.tar.bz2

Explanation:

  • -x → extract files
  • -j → use bzip2 decompression
  • -f → specify archive file

Extract to a specific directory:

text
tar -xjf backup.tar.bz2 -C /path/to/destination

List contents without extracting:

text
tar -tjf backup.tar.bz2

Frequently Asked Questions

1. What is the bzip2 command in Linux?

The bzip2 command in Linux is a file compression utility that uses the Burrows–Wheeler algorithm and Huffman coding to reduce file size. It provides better compression than gzip but is usually slower.

2. How do I decompress a .bz2 file in Linux?

You can decompress a .bz2 file using either bzip2 -d filename.bz2 or the bunzip2 filename.bz2 command.

3. Does bzip2 compress directories?

No, bzip2 can compress only files. To compress directories, you must first archive them using the tar command and then compress the archive with bzip2.

4. What is the difference between bzip2 and gzip?

bzip2 generally provides a better compression ratio than gzip, resulting in smaller files, but gzip is faster for compression and decompression.

5. What is the default compression level of bzip2?

By default, bzip2 uses compression level 6, which balances compression speed and compression ratio.

Conclusion

The bzip2 command is a reliable and efficient compression tool in Linux, particularly when disk space optimization is a priority. With options for compression control, integrity testing, and pipeline integration, bzip2 fits well into both interactive use and automation scripts.


What’s Next


Further Reading

man page for bzip2 command

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.