FUNDAMENTALS A Complete Guide for Beginners
bzip2 is a command in Linux that is used to compress & decompress files. It uses the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding to compress files. In this section, you will get to know the ins and outs of the bzip2 command in Linux.
A. Description
bzip2 command in Linux compresses files & decompresses compressed files. Using the bzip2 command, file size is reduced without losing data This command is also used to check the integrity of the compressed files. Unlike other compression commands, it replaces the original file while compression.
B. Syntax
The bzip2 command has a very simple syntax in Linux. It includes the bzip2 command, options & filename. The syntax is as follows:
bzip2 [-OPTION]... [FILE]...
C. Options
A good number of options are available for the bzip2 command. I have listed here some most useful options of the command. Nevertheless, you can explore the man page for the bzip2 command to know more about its options.
man bzip2
Useful Options
- -z, Forces compression of file and removes the original file.
- -k, Compresses file but does not remove the original file.
- -d, Decompresses file that is compressed.
- -t, Tests compressed file’s integrity, does not decompress the file.
- -v, Shows the compression ratio of the file.
- -L or -V, Displays the software version, license terms, and conditions.
Practical Examples of the “bzip2” Command in Linux
The bzip2 command in Linux is a pretty straightforward command that will compress or decompress a file based on which options are used. Here you will learn several useful examples of the bzip2 command.
Example 1: Force Compression of File Using the “bzip2” Command in Linux
To compress a file named a.txt I am using the bzip2 command along with option -z. It forces the compression of the file and removes the original file. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls
This will show the uncompressed file.
➌ Now, press the ENTER button.
❹Type the following command in the command prompt:
bzip2 -z a.txt
➎ Now, press the ENTER button.
❻Type the following command in the command prompt:
ls
This will display the compressed file here.
❼ Finally, press the ENTER button.
Output >
In the image below, you can see the file is compressed but the original file no longer exists.
Example 2: Compress Multiple Files Using the “bzip2” Command in Linux
I am using the bzip2 command along with option -z to compress multiple files named b.txt, c.txt & d.txt. Option -z forces the compression of the file and replaces the original file. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls
This will show all the uncompressed files.
➌ Now, press the ENTER button.
❹Type the following command in the command prompt:
bzip2 -z b.txt c.txt d.txt
➎ Now, press the ENTER button.
❻Type the following command in the command prompt:
ls
This will display all the compressed files here.
❼ Finally, press the ENTER button.
Output >
In the following image, you can see multiple files are compressed but the original files no longer exist.
Example 3: Compress Files Keeping the Original Files
To compress files without replacing the original files you can use the bzip2 command with option -k.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls
This will show all the uncompressed files.
➌ Now, press the ENTER button.
❹Type the following command in the command prompt:
bzip2 -k b.txt c.txt
➎ Now, press the ENTER button.
❻Type the following command in the command prompt:
ls
This will display all the compressed files here along with the original files.
❼ Finally, press the ENTER button.
Output >
In the image below, the files are compressed and the original files are not replaced by them.
Example 4: Decompress Files Using the “bzip2” Command in Linux
To decompress files named b.txt & c.txt, I am using the bzip2 command along with option -d. This decompresses the files replacing the original files. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
ls
This will show all the compressed files.
➌ Now, press the ENTER button.
❹Type the following command in the command prompt:
bzip2 -d b.txt.bz2 c.txt.bz2
➎ Now, press the ENTER button.
❻Type the following command in the command prompt:
ls
This will display all the compressed files here.
❼ Finally, press the ENTER button.
Output >
As you can see in the output, the files are decompressed and the compressed files are removed.
Example 5: Test Integrity of Compressed File Using the “bzip2” Command in Linux
To test the integrity of the compressed file b.txt.bz2, I am using the bzip2 command as well as option -t. To do the same you can follow the procedure below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ To show all the compressed files in the current directory, type the following command in the command prompt:
ls
➌ Now, press the ENTER button.
❹ Type the following command in the command prompt:
bzip2 -t b.txt.bz2
This will not display anything as the file is genuine whereas if the file was corrupted, it would show an error.
➎ To create a file named a.txt.bz2, type the following command in the command prompt:
touch a.txt.bz2
❻ Now, press the ENTER button.
❼ To check if the file is created or not, type the following command in the command prompt:
ls
❽ Then, press the ENTER button.
❾ To check the integrity of a.txt.bz2 file, type the following command in the command prompt:
bzip2 -t a.txt.bz2
❿ Finally, press the ENTER button.
Output >
In the following image, a.txt.bz2 file is corrupted as the output displays an error whereas the b.txt.bz2 file is genuine and there’s no error.
Example 6: Display the Compression Ratio of the Specified File
To display the file’s compression ratio, you can use the bzip2 command with option -v. This is basically called verbose mode. You can find a lot of information in this mode which can be used for diagnostic purposes.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ To display the compression ratio of the file, type the following command in the command prompt:
bzip2 -v b.txt
❸ Now, press the ENTER button.
Output >
As you can see here, the output displays the compression ratio of the file.
Example 7: Display the Software Version, Licence Terms, and Conditions
You can use the bzip2 command with option -V or -L to display the software version, license terms, and conditions in Linux.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ To display the compression ratio of the file, type the following command in the command prompt:
bzip2 -V
Or,
bzip2 -L
❸ Then, press the ENTER button.
Output >
As you can see, the outputs display the software version, license terms, and conditions. Or,
Conclusion
As demonstrated in this section, the bzip2 command has a lot of uses in Linux. You’ve also discovered the syntax, some functional options, and the practical application of this command. To become a pro in Linux, explore the command and its practical applications thoroughly.
Similar Readings
- The “zip ” Command in Linux [9 Practical Examples]
- The “unzip” Command in Linux [7 Practical Examples]
- The “gzip” Command in Linux [9 Practical Examples]
- The “gunzip” Command in Linux [9 Practical Examples]