Linux bzip2 Command Tutorial for Beginners (6 Examples)
On this page
- Linux bzip2 command
- Q1. How to compress a file using bzip2?
- Q2. How to compress multiple files using bzip2?
- Q3. How to decompress using bzip2?
- Q4. How to make bzip2 not delete input file?
- Q5. How to make bzip2 display details for each compression operation?
- Q6. How to check integrity of a compressed file?
- Conclusion
File compressions are carried out according to specific algorithms. There are many compression techniques, and one of them is achieved through bzip2. In this tutorial, we will learn the basics of bzip2 using some easy to understand examples. Please note that all examples used in this article have been tested on an Ubuntu 22.04LTS machine.
Linux bzip2 command
bzip2 is a command line based file compressor in Linux that uses the Burrows-Wheeler block sorting text compression algorithm and Huffman coding to carry out the compression process. Following is its syntax:
bzip2 [OPTIONS] filenames ...
And here is what the man page says about this tool:
bzip2 compresses files using the Burrows-Wheeler block sorting text
compression algorithm, and Huffman coding. Compression is generally
considerably better than that achieved by more conventional
LZ77/LZ78-based compressors, and approaches the performance of the PPM
family of statistical compressors.
The command-line options are deliberately very similar to those of GNU
gzip, but they are not identical.
bzip2 expects a list of file names to accompany the command-line flags.
Each file is replaced by a compressed version of itself, with the name
"original_name.bz2". Each compressed file has the same modification
date, permissions, and, when possible, ownership as the corresponding
original, so that these properties can be correctly restored at decom?
pression time. File name handling is naive in the sense that there is
no mechanism for preserving original file names, permissions, owner?
ships or dates in filesystems which lack these concepts, or have seri?
ous file name length restrictions, such as MS-DOS.
Following are some Q&A-styled examples that should give you a good idea on how the bzip2 command works.
Q1. How to compress a file using bzip2?
Basic usage is very simple - just pass the file to be compressed as input to the bzip2 command. Here's is an example:
bzip2 list.txt
The following screenshot shows the command in action:
Q2. How to compress multiple files using bzip2?
Simple - just pass the filenames as input. See the following example:
bzip2 list.txt list1.txt list2.txt
Q3. How to decompress using bzip2?
For decompression, use the -d command line option. Here's an example:
bzip2 -d list.txt.bz2
Q4. How to make bzip2 not delete input file?
By default, when bzip2 compresses a file, it deletes the original (or input) file. However, if you don't want that to happen, use the -k command line option.
Following is an example:
Q5. How to make bzip2 display details for each compression operation?
This can be done using the -v command line option. Here's how the man page explains it:
-v --verbose
Verbose mode -- show the compression ratio for each file processed. Further -v's
increase the verbosity level, spewing out lots of information which is primarily of
interest for diagnostic purposes.
Following is an example that shows bzip2 command output when -v is used:
Q6. How to check integrity of a compressed file?
The bzip2 command can also be used to check the integrity of a .bz2 file (a test that makes sure the file isn't corrupt or has changed since it was created). This can be done using the -t command line option.
-t --test
Check integrity of the specified file(s), but don't decompress them.
This really performs a trial decompression and throws away the result.
Conclusion
The bzip2 command line utility offers many more options, but whatever we have discussed here should be enough to get you started. Once you're done practicing the options we've discussed in this tutorial, you can head to the tool's man page to learn more about it.