In this article you will learn how to compress files using the bz2 file compression tool (Linux command bzip2) and how to decompress the bz2 compressed file. bzip2 is an open source compression program similar to the zip & gzip compression program that compresses large files to reduce their size. bzip2 cannot compress directories. bzip2 offers more compression than gzip and zip. The Linux command bzip2 was developed by Julian Seward in 1996 and published under a BSD license.
Why do we have to compress a file. Let’s take an example. Suppose you want to send an email to someone with an attachment. However, your attachment file is larger than the allowable attachment size. In this case, you can use the compression tool for bz2 files (Linux command bzip2) to compress the file and reduce the size of the file. Some of the main features of the bz2 file compression tool include:
- It is a cross-platform application that is available for the most important operating systems, e.g. B. Linux, Microsoft Windows and MacOS.
- Can recover data from damaged bz2 file.
- Available for 32-bit and 64-bit operating systems.
- It can create fast and best compression of the files.
Compress files with Bz2 (Bzip2 Linux command) and unzip the Bz2 file on Linux:
Now let’s take a look at the bzip2 Linux command with the following examples:
Compress a file
Compress a file with the Linux command bzip2.
[email protected]ubuntu:~/data$ bzip2 myfile.txt # Compress a File # Output [email protected]:~/data$ ls myfile.txt.bz2
Compress a file with standard output
bzip2 command with -c compresses the file with standard output.
[email protected]:~/data$ bzip2 -c myfile.txt > myfile.txt.bz2 # Compress a file with standard output # Output [email protected]:~/data$ ls myfile.txt myfile.txt.bz2
Compress a file by keeping the input file (source file).
Typically, the bzip2 command compresses the file and deletes the source file, but the bzip2 command with an argument -k compresses the file by not deleting the source file.
[email protected]:~/data$ bzip2 -k myfile.txt # Compress file without delete Input file # Output [email protected]:~/data$ ls myfile.txt myfile.txt.bz2
Compress multiple files at the same time
The bz2 file compression tool (Linux command bzip2) can compress several files at the same time. To do this, use the following command.
[email protected]:~/data$ bzip2 myfile.txt myfile1.txt myfile2.txt myfile3.txt # Compress multiple files at once # Output [email protected]:~/data$ ls myfile1.txt.bz2 myfile2.txt.bz2 myfile3.txt.bz2 myfile.txt.bz2
Read also: Best Linux Gzip Command (Gzip Compression) with Examples
Check the integrity of a specified file
bzip2 Linux command with argument -t Checks the integrity of a specified bz2 file. Integrity is the feeling that the bz2 file is a valid file or not. You can use the following command to do this. If the file is valid, you will get no output.
[email protected]:~/data$ bzip2 -t myfile.txt.bz2 # Check the Integrity of the bz2 file
If the bz2 file is not a valid file, an error message is displayed. To prove this, we create a bzip2 file with the touch command and then check the integrity.
[email protected]:~/data$ touch myfile.txt.bz2 [email protected]:~/data$ bzip2 -t myfile.txt.bz2 bzip2: myfile.txt.bz2: file ends unexpectedly You can use the `bzip2recover' program to attempt to recover data from undamaged sections of corrupted files.
As you can see above, we received the error because we did not create this file with the bzip2 Linux command because it is not a valid bz2 file.
Decompress / extract / unzip the bz2 file
Command bzip2 with argument -d decompresses the bzip2 file.
[email protected]:~/data$ bzip2 -d myfile.txt.bz2 # Uncompress a file # Output [email protected]:~/data$ ls myfile.txt
You can also unzip the bz2 file with the bunzip2 command. See the following command.
[email protected]:~/data$ bunzip2 myfile.txt.bz2 # Output [email protected]:~/data$ ls myfile.txt
Check the contents of a bz2 compressed file with the bzcat command
You can check the contents of a compressed file without extracting it. To do this, we have to use the bzcat command. See the following command.
[email protected]:~/data$ bzcat myfile.txt.bz2 # Check content of a Compressed file Welcome to itsmarttricks.com
Compress a file by force
Command bzip2 with argument -f creates a bz2 file with emphasis. See the following command.
[email protected]:~/data$ bzip2 -f myfile.txt # Compress a file forcefully [email protected]:~/data$ ls myfile.txt.bz2
Read also: Examples of TAR commands on Linux
Compress a directory using the bz2 file compression tool
We cannot compress a directory with the only Linux bzip2 command, but a directory can be compressed with the bzip2 command with the tar command. Use the following command to do the same.
[email protected]:~/data$ tar -cjvf data.tar.bz2 data/ # Compress a Directory data/ data/myfile3.txt data/myfile2.txt data/myfile1.txt # Output [email protected]:~/data$ ls data data.tar.bz2
Here argument j is for bzip2 compression.
There are 9 levels of compression available in each compression tool. The levels are 1.2… 9. Here I show you two levels. i.e. Level 1 & Level 9.
Level 1 offers fast compression output. See the following command.
[email protected]:~/data$ bzip2 -1 myfile.txt # 1 for Fast Compression
Level 9 offers the best compression performance. See the following command.
[email protected]:~/data$ bzip2 -9 myfile.txt # 9 for Best Compression
Check the license & package version of the Linux command bzip2
Use the bzip2 command with the argument -L to check the License & Package version of the bz2 file compression tool.
[email protected]:~/data$ bzip2 -L # Check Version & License bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010. Copyright (C) 1996-2010 by Julian Seward. This program is free software; you can redistribute it and/or modify it under the terms set out in the LICENSE file, which is included in the bzip2-1.0.6 source distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for more details.
Read also: How to install the 7Zip (7z) Archive Tool in Ubuntu 18.04
For more help and information about the bzip2 Linux command, use the following command.
[email protected]:~/data$ man bzip2 # Access bzip2 manual page
That’s all. In this article we explained the use of “compress files” bz2 (Bzip2 Linux command) and unzip Bz2 file in Linux. I hope you like this article. If you like this article, just share it. If you have any questions about this article, please comment.