Linux bzip2 Command
The Linux bzip2 command is used to compress files.
Similar to gzip, bzip2 provides a better compression ratio by using a more efficient compression algorithm (Burrows-Wheeler algorithm), but is usually slightly slower at compressing.
Syntax
bzip2 [options] [file...]
file...: the file to compress.bzip2compresses the specified file and generates a.bz2compressed file with the suffix, while deleting the original file.
options common parameters:
-d: decompress a.bz2file. Equivalent to usingbunzip2command.-k: keep the original file, do not delete it.-v: display the detailed compression or decompression process.-z: force compression, even if the file is already compressed.-t: test the integrity of the compressed file.-1to-9: specify the compression ratio.-1is the fastest compression, with the lowest compression ratio;-9is the slowest compression, with the highest compression ratio. The default is-9.
Example
Compress a file
bzip2 example.txt
This command compresses example.txt into example.txt.bz2 and deletes the original file example.txt.
Keep the original file
If you want to keep the original file after compression, you can use the -k option:
bzip2 -k example.txt
This command keeps the original example.txt file and generates example.txt.bz2.
Decompress a file
To decompress a .bz2 file, you can use the -d option or use bunzip2 directly:
bzip2 -d example.txt.bz2
or
bunzip2 example.txt.bz2
This decompresses example.txt.bz2 back into the original example.txt file.
** Test compressed files**
You can use the -t option to test the integrity of a compressed file:
bzip2 -t example.txt.bz2
If the file is intact and undamaged, the command produces no output; otherwise, it reports an error.
Linux Command Reference