bzip2 command in Linux with Examples
bzip2 command in Linux is used to compress and decompress the files i.e. it helps in binding the files into a single file which takes less storage space than the original file used to take. It has a slower decompression time and higher memory use. It uses Burrows-Wheeler block sorting text compression algorithm, and Huffman Coding. Each file is replaced by a compressed version of itself, with the name original name of the file followed by the extension bz2.
Syntax
bzip2 [OPTIONS] filenames ...
Commonly Used Options in bzip2
1. Compress a File (-z Option)
The -z option forces compression, though it is the default action of the bzip2 command. When you run this command, the original file is replaced by the compressed version.
$ bzip2 -z input.txt

2. Keep the Original File (-k Option)
Normally, bzip2 deletes the original file after compression, but the -k option ensures the original file is preserved alongside the compressed version.
$ bzip2 -k input.txt

3. Decompress a File (-d Option)
The -d option is used for decompressing files that were previously compressed using bzip2.
$ bzip2 -d input.txt.bz2

4. Integrity Check (-t Option)
If you want to check whether a .bz2 file is corrupted without decompressing it, the -t option comes in handy. It checks the integrity of the file and informs you if it's corrupted.
$ bzip2 -t input.txt.bz2

5. Verbose Mode (-v Option)
The -v option enables verbose mode, where the command shows additional information, such as compression ratios and other diagnostics, during the compression process.
$ bzip2 -v input.txt

Other Available Options
Option | Description |
---|---|
-h , --help | Displays the help message and exits. |
-L , --license | Displays the software version, license terms, and conditions. |
-V , --version | Displays the software version and exits. |
-q , --quiet | Suppresses non-essential warning messages. Critical messages like I/O errors are still displayed. |
-f , --force | Forces overwriting of output files without confirmation. |
Conclusion
The bzip2 command is an essential tool for compressing files in Linux, especially when storage space is a concern. While it offers excellent compression ratios, its higher memory usage and slower decompression times make it better suited for specific use cases, such as long-term storage or data transfers where space efficiency is critical.