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

Compress a File
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

Keep the Original File
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

Decompress a File
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

Integrity Check
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

Verbose Mode
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.
bzip2 command in Linux – FAQs
What is the main difference between gzip and bzip2?
bzip2 generally provides better compression ratios than gzip, but it tends to be slower, especially during decompression, and uses more memory.
Does bzip2 delete the original file after compression?
Yes, by default, bzip2 deletes the original file after compression. To keep the original file, use the -k option.
How can I check if a .bz2 file is corrupt?
Use the -t option to perform an integrity check on the file. This checks for any corruption without decompressing the file.
What is the .bz2 extension?
The .bz2 extension indicates that a file has been compressed using the bzip2 compression algorithm.