LinuxCommandLibrary

bzip2

Compress files using block-sorting lossless algorithm

TLDR

Compress a file

$ bzip2 [path/to/file_to_compress]
copy

Decompress a file
$ bzip2 [[-d|--decompress]] [path/to/compressed_file.bz2]
copy

Decompress a file to stdout
$ bzip2 [[-dc|--decompress --stdout]] [path/to/compressed_file.bz2]
copy

Test the integrity of each file inside the archive file
$ bzip2 [[-t|--test]] [path/to/compressed_file.bz2]
copy

Show the compression ratio for each file processed with detailed information
$ bzip2 [[-v|--verbose]] [path/to/compressed_files.bz2]
copy

Decompress a file overwriting existing files
$ bzip2 [[-f|--force]] [path/to/compressed_file.bz2]
copy

Display help
$ bzip2 [[-h|--help]]
copy

SYNOPSIS

bzip2 [-c|-d] [-fkqstv] [-1|-2|...|-9] [--repetitive-{best|fast}] [filenames...]

PARAMETERS

-1 .. -9
    Set compression block size: -1 (fastest, 100kB) to -9 (best ratio, 900kB)

-c, --stdout
    Compress or decompress to standard output; keep original files

-d, --decompress
    Force decompression (.bz2 files)

-f, --force
    Overwrite existing files; ignore corrupted input

-k, --keep
    Keep input files after compression/decompression

-q, --quiet
    Suppress non-critical warnings/errors

-s, --small
    Use less memory (~ half of normal)

-t, --test
    Test integrity of .bz2 files without decompression

-v, --verbose
    Verbose mode; multiple -v increase detail

-L, --license
    Display software license

-V, --version
    Display version information

--repetitive-best/-9
    Optimize for repetitive data (alias for -9)

--repetitive-fast/-1
    Fast mode for repetitive data (alias for -1)

DESCRIPTION

bzip2 is a high-quality, patent-free compression program using the Burrows-Wheeler transform combined with Huffman coding and move-to-front transformation. It achieves compression ratios typically 10-15% better than gzip, making it ideal for archiving large datasets where space is critical. Compression is slower but decompression is significantly faster—up to 6x quicker than similar tools.

The tool processes one or more files, appending a .bz2 extension to compressed outputs. It supports streaming via stdin/stdout, enabling pipelines like tar cf - dir | bzip2 > archive.tar.bz2. Block sizes from 100kB to 900kB balance speed vs. ratio; larger blocks improve compression but demand more memory (up to 3700kB for -9).

Related symlinks include bunzip2 (decompress), bzcat (decompress to stdout), and bzmore (view). It's widely used in Linux distros for .tar.bz2 archives. While memory-hungry for huge files, it's reliable and CPU-efficient for decompression. Source code and libbzip2 library are available for integration.

CAVEATS

High memory use for large blocks/files (e.g., 3700kB for -9 on 900kB block); slower compression than gzip; not suited for real-time streaming due to latency.

MULTI-FILE HANDLING

Processes multiple files sequentially; creates .bz2 copies. Use -c for tar+bzip2 pipelines.

STDIN MODE

bzip2 -c < file.txt > file.bz2 or bzcat file.bz2 for extraction.

HISTORY

Developed by Julian Seward; first public release 1996. Stable v1.0.6 (2010), latest 1.0.8 (2019). Widely adopted for superior ratio vs. gzip; libbzip2 enables programmatic use.

SEE ALSO

gzip(1), xz(1), bunzip2(1), bzcat(1), tar(1)

Copied to clipboard