The bzip2 command is used to compress and decompress files in Linux. It reduces file size by creating compressed .bz2 files, which take less storage than the original files. Uses the Burrows–Wheeler block-sorting algorithm and Huffman coding for compression.
- Replaces the original file with a .bz2 version by default.
- Slower decompression time and higher memory usage compared to gzip.
- Can retain the original file using the -k option.
- Suitable for storage optimization and data transfer.
Example: Compressing a File While Keeping the Original
Command:
bzip2 -k file1.txt- -k: Keeps the original file intact. Without -k, file1.txt would be removed.
Output:

Syntax
bzip2 [OPTIONS] filenames ...- filenames: One or more files to compress individually.
- Compressed files are named <original>.bz2.
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.
Command:
bzip2 -z input.txtOutput:

2. Decompress a File (-d Option)
The -d option is used for decompressing files that were previously compressed using bzip2.
Command:
bzip2 -d input.txt.bz2Output:

3. 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.
Command:
bzip2 -t input.txt.bz2Output:

4. 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.
Command:
bzip2 -v input.txtOutput:

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. |