Compress and Decompress a . bzip2 File in Linux

Linux Tech Expert
2 min readSep 12, 2021

--

In this tutorial, we will look at how to compress and decompress .bz2 files using the bzip2 tool in Linux. To compress a file(s), is to significantly decrease the size of the file(s) by encoding data in the file(s) using less bits, and it is normally a useful practice during backup and transfer of a file(s) over a network. On the other hand, decompressing a file(s) means restoring data in the file(s) to its original state.

Suggested Read: Learn Linux ‘tar’ Command with This 18 Examples

There are several file compression and decompression tools available in Linux such as gzip, 7-zip, Lrzip, PeaZip and many more.

Bzip2 is a well known compression tool and it’s available on most if not all the major Linux distributions, you can use the appropriate command for your distribution to install it.

$ sudo apt install bzip2     [On Debian/Ubuntu] 
$ sudo yum install bzip2 [On CentOS/RHEL]
$ sudo dnf install bzip2 [On Fedora 22+]

The conventional syntax of using bzip2 is:

$ bzip2 option(s) filenames

How to Use “bzip2” to Compress Files in Linux

You can compress a file as below, where the flag -z enables file compression:

$ bzip2 filename
OR
$ bzip2 -z filename

To compress a .tar file, use the command format:

$ bzip2 -z backup.tar

Important: By default, bz2 deletes the input files during compression or decompression, to keep the input files, use the -k or --keep option.

In addition, the -f or --force flag will force bz2 to overwrite an existing output file.

------ To keep input file  ------
$ bzip2 -zk filename
$ bzip2 -zk backup.tar

You can as well set the block size to 100k upto 900k, using -1 or --fast to -9 or –best as shown in the below examples:

$ bzip2 -k1  Etcher-linux-x64.AppImage
$ ls -lh Etcher-linux-x64.AppImage.bz2
$ bzip2 -k9 Etcher-linux-x64.AppImage
$ bzip2 -kf9 Etcher-linux-x64.AppImage
$ ls -lh Etcher-linux-x64.AppImage.bz2

The screenshot below shows how to use options to keep the input file, force it to overwrite an output file and set the block size during compression

for more useful information about linux visit site :http://linuxtechexpert.com/

--

--