bzip2

file compressionlinux
The bzip2 command is one of the most frequently used commands in Linux/Unix-like operating systems. bzip2 The bzip2 command compresses files using the Burrows-Wheeler block sorting text compression algorithm and Huffman coding. It generally achieves better compression than gzip but is slower.

Quick Reference

Command Name:

bzip2

Category:

file compression

Platform:

linux

Basic Usage:

bzip2 [options] [arguments]

Common Use Cases

  • 1

    File compression

    Compress files to save storage space

  • 2

    Archive creation

    Create compressed archives of files and directories

  • 3

    Data backup

    Create compressed backups of important data

  • 4

    Storage optimization

    Reduce storage requirements through compression

Syntax

bzip2 [options] [filenames...]
bunzip2 [options] [filenames...]

Options

Option Description
-c, --stdout Write to standard output and keep original files
-d, --decompress Force decompression (same as using bunzip2)
-z, --compress Force compression (default action)
-t, --test Test compressed file integrity
-f, --force Overwrite existing output files
-k, --keep Keep (don't delete) input files during compression/decompression
-s, --small Reduce memory usage (at the expense of compression ratio)
-q, --quiet Suppress non-error messages
-v, --verbose Show compression ratio for each file processed
-1 to -9 Set compression level: 1=fastest (less compression), 9=best (slowest)
--fast Same as -1 (fastest compression)
--best Same as -9 (best compression)
-L, --license Display software license
-V, --version Display version number
-h, --help Display help message

Examples

How to Use These Examples

The examples below show common ways to use the bzip2 command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Compress a file (original file is replaced by compressed version)
bzip2 filename.txt
# Creates filename.txt.bz2 and removes filename.txt
Compress a file but keep the original file
bzip2 -k filename.txt
# Creates filename.txt.bz2 and keeps filename.txt
Decompress a file
bunzip2 filename.txt.bz2
# Creates filename.txt and removes filename.txt.bz2
Show compression details
bzip2 -v filename.txt
# Displays compression ratio and other information

Advanced Examples:

Compress multiple files at once
bzip2 file1.txt file2.txt file3.txt
Compress with highest compression level
bzip2 -9 largefile.txt
Compress with fastest compression level
bzip2 -1 largefile.txt
Test integrity of a compressed file
bzip2 -t archive.bz2
Compress a file to standard output
bzip2 -c filename.txt > archive.bz2
Compress from standard input to a file
cat file.txt | bzip2 > file.txt.bz2
Force compression even if the file already exists
bzip2 -f filename.txt
Compress all files in a directory with bzip2 (preserving originals)
find . -type f -name "*.log" -exec bzip2 -k {} \;
Create a compressed tar archive
tar -cjf archive.tar.bz2 directory/
Decompress to standard output
bunzip2 -c file.bz2 > decompressed_file

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Compression Algorithm:

bzip2 uses a combination of techniques to achieve high compression ratios:

  • Burrows-Wheeler Transform (BWT) for text compression
  • Move-to-front transform to improve compression efficiency
  • Huffman coding for final encoding
  • Typically compresses files to 10-15% smaller than gzip
  • Uses blocks of 100k-900k bytes, sorted and compressed independently

File Naming Conventions:

  • bzip2 appends the .bz2 extension to compressed files
  • When compressing a file, the original file is normally deleted
  • If you want to keep the original file, use the -k option
  • If filename already has .bz2 extension, it's not changed

Related Commands and Utilities:

  • bunzip2: Decompresses .bz2 files (same as bzip2 -d)
  • bzcat: Decompresses .bz2 files to standard output (like bunzip2 -c)
  • bzip2recover: Recovers data from damaged .bz2 files
  • bzgrep: Searches compressed files without full decompression
  • bzmore/bzless: Views compressed files without full decompression
  • bzdiff: Compares compressed files

Performance Considerations:

  • bzip2 is generally slower than gzip but provides better compression
  • Memory usage is higher than gzip but lower than more advanced compressors
  • Compression levels (-1 to -9) allow balancing speed vs. compression ratio
  • Decompression speed is relatively consistent regardless of compression level
  • The -s (small) option reduces memory usage at the cost of compression speed

Common Usage Patterns:

  • Use with tar (tar -cjf) to create compressed archives
  • Compress log files to save disk space
  • Use for backups where storage efficiency is more important than speed
  • Stream compression using stdin/stdout for integration with pipes
  • Preserving file timestamps and permissions requires additional tools

Compatibility Notes:

  • bzip2 files are not compatible with gzip or other compression formats
  • Available on virtually all Unix/Linux systems and can be installed on Windows
  • The .bz2 format is widely supported across platforms
  • Newer alternatives like xz and zstd may offer better performance or compression

Common Use Cases

File compression

Compress files to save storage space

Archive creation

Create compressed archives of files and directories

Data backup

Create compressed backups of important data

Storage optimization

Reduce storage requirements through compression

Data transfer

Compress files for efficient data transfer

Related Commands

These commands are frequently used alongside bzip2 or serve similar purposes:

Use Cases

1

File compression

Compress files to save storage space

2

Archive creation

Create compressed archives of files and directories

3

Data backup

Create compressed backups of important data

4

Storage optimization

Reduce storage requirements through compression

5

Data transfer

Compress files for efficient data transfer

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the bzip2 command works in different scenarios.

$ bzip2
View All Commands
bzip2 - Linux Command Guide