How to Use Bzip2 to Compress Files in Linux

Wondering how to use bzip2 to compress files in Linux?

Bzip2 is a handy tool that aids in compressing the size of a given file substantially and decompressing it efficiently. This can come in handy when you have to transfer a bulk of large files or transfer files during a backup procedure. 

The software is open-source and it’s available on almost all distributions of Linux. It is becoming more and more popular among Linux users due to its cross-platform flexibility and outstanding results. While it does not have built-in archive facilitation, using tools like tar, you can compress multiple files into archives.

If you wish to learn how to use Bzip2 to compress files in Linux, then keep reading.

So without further ado, let’s begin!

Bzip Installation.

It should be noted that Bzip2 was released in 1996, so it’s safe to say it can be easily installed on most versions of Ubuntu. The package can be found in the Ubuntu Package Repository, which means you can download it via the Terminal using the “apt” command. We’ll discuss how to do that.

First and foremost, log in to your system and open the Terminal window. Once there, go through the usual yet salient practice of updating our system with the latest repositories. To do that, type the following in the Command Terminal:

$ sudo apt-get update 

After feeding in your password, the Terminal will install the latest data packages.

Next, type the following in the Terminal:

$ sudo apt-get install bzip2

This will initiate the installation of the Bzip2 package on your system. Once the process is completed, the command should be available for use.

Bzip2 to Compress Files in Linux

Now that you have Bzip2 installed on your system, you can proceed to learn how to use the command to compress files in Linux. With this, you’ll be able to compress and decompress files when the need arises. 

You can now proceed to test the functionality and variations of the Bzip2 command.

Compressing a file.

Suppose you have a file “hekati” with the webp extension located on your desktop. You’ll first need to change to the appropriate directory using the $cd command. Once there, you’ll type in the generic command:

$ bzip <filename>

This will get a zipped file made for you. It should be noted that making the zip file will compress the original file without making a copy unlike Winrar does for Windows.

To illustrate:

$ cd Desktop

$ bzip2 hakati.webp
virtual box
Bzip2 to Compress Files in Linux

In several cases, you may need the original file to keep different versions for modification purposes. Bzip2 allows that. It also allows you to compress multiple files at once to save time.

Retain original file.

To compress multiple files while retaining the original file at the same time, just follow the generic sequence given below:

$ bzip2 -c file.txt> file2.txt>
Bzip2 to Compress Files in Linux
Bzip2 to Compress Files in Linux

As you can see, we now have compressed files along with their editable original.

The -c flag keeps the original file in its place. Alternatively, in the newer versions of Bzip2, there is an additional flag -k that performs the same task:

$ bzip2 -k <filename along with extension>

For instance:

$ bzip2 -k picture1.webp>

Will create a new file while retaining the original picture1.webp file.

Concatenating files.

Concatenating is the process of storing multiple files in a single file. This is used when you want to join two or more text strings into one string. You can concatenate files using these commands:

$ bzip2 -c file1.webp>files.bz2

$ bzip2 -c file2.webp>files.bz2

As a result of this, files.bz2 will now contain content of both file1.webp and file2.webp.

Display detail.

The bzip2 command can also show the details of compressing certain files. All you have to do is add a -v or –verbose flag with it as shown below:

$ bzip2 -k -v picture1.webp
virtual box

Test flag.

You can use the test flag to check the authenticity of a given file. This will give you an idea as to whether the file was created by Bzip2 or not, and if it is damaged or corrupted.

$ bzip2 -k -t picture1.webp

As you can see, the tool also recommends a program that can recover compiled files or damaged sections. Impressive indeed!

Forced compression.

You can force a compression using the -z flag. This can be used in case of emergencies or when you need swift action for tasks like reducing memory consumption.

Use the following command:

$ bzip2 -k -z picture1.webp

$ ls
virtual box ubuntu

In addition to the ones given here, there are several other variations to the command alongside flags that can give you a wider range of options for effective compression. You can find these variations as well as other useful information on the man page for this command.

Decompress a given file.

You can decompress one or multiple files using the -d  flag or the bunzip command. The syntax is as follow:

$ bunzip2 picture1.webp.bz2

$ ls

$ bzip2 -d picture1.webp.bz2

This is what the file looks like before compression:

picture 1

This is what the file should look like after compression:

picture 1

The difference between the two formats is that the bunzip command extracts all relevant files including “.tar” files and the extracted files have format filename.tar.

Advantages of Bzip2.

  • Better compression than other tools like gzip and zip.
  • Uses the Burrows-Wheeler algorithm, which allows for greater ratio compressions.
  • Available on 32-bit and 64-bit distributions.
  • Can run on various platforms—Linux, Mac, and Windows.
  • Can compress multiple files at once.
  • Enables data retrieval from several compiled files. 

Whether it be recovering corrupted files or compressing files with an astounding ratio, Bzip2 is a nifty tool that can help us a long way. It surpasses other tools like gzip and offers more versatility. Sure it may have a relatively slower decompression rate but that is a negligible trade-off for superior quality.

Compression isn’t just limited to individual files. Many File Systems use compression as a way to save storage space. An example of such a File System is ZFS or OpenZFS. Learning ZFS compression can be handy if you’re short on storage space.

We hope this guide helped you learn how to use bzip2 to compress files in Linux. If you think this guide was of use to you or if you have any other suggestions, let us know in the comment section down below.


Leave a Reply

Your email address will not be published. Required fields are marked *