Bzip2 for Beginners : How to use Bzip2 to Compress Files in Linux/Unix Based Systems

Bzip2 is a free and open-source file compression program that uses the Burrows–Wheeler algorithm to compress files. The data format BZIP2 was created for the compression of single or concatenated files. Structure and usage of BZIP2 are similar to the older GZIP format. BZIP2 was developed as alternative to GZIP and offers improved compression.

Bzip2 is implemented as the traditional alternative to gzip. While gzip relies on the "DEFLATE" algorithm, bzip2 is an implementation of an algorithm called the "Burrows-Wheeler algorithm". This difference in methodology results in a set of strengths and weaknesses that is quite different from gzip. The most important trade off for most users is greater compression at the cost of longer compression time. The bzip2 tools can create significantly more compact files than gzip, but take much longer to achieve those results due to a more complex algorithm. The bzip2 command compress the files to their corresponding bzip2 file, by default replacing the original file by compressed one. Although the compressed file retain the permissions, modification date and ownership of the original files, when possible.

Now lets take a look of some basic use of bzip2 to compress files :

First check that the bzip2 is installed or not on your system :
 $ which bzip2
if bzip2 is installed on your system then it will return the PATH of wget /bin/bzip2. or if it not installed then nothing will return. Now to install bzip2 use the below command :
 $ sudo apt-get update
 $ sudo apt-get install bzip2
1. Compress a file :

To compress a file with bzip2 use the command below :
 $ bzip2 [file-name]
example :
 $ bzip2 document.dat
By default in compression process, the original file is replaced by the compressed file. So, to keep the original file also use the -k option
 $ bzip2 -k [file-name]
2. Compressing multiple files :

To compress multiple files at once just provide the file names to bzip2
 $ bzip2 [file_name1] [file_name2] [file_name3]
3. Verbose option :

To see the details of compression use -v option :
 $ bzip2 -v [file-name]
example :
 $ bzip2 -v document.dat
4. Custom name for compressed files :

To compressed files with custom names use -c option :
 $ bzip2 -c [file-name] > [compressed-file-name]
example :
 $ bzip2 -c document.dat > docfile.dat.bz2
5. To decompress files :

To decompress a file with in bzip2, use -d option :
 $ bzip2 -d [compressed-file-name]
example :
 $ bzip2 -d document.dat.bz2
And if you want to keep the compress file then put -k option :
 $ bzip2 -dk document.dat.bz2
6. Test The Validity Of A "bz2" File

To test the validity of file compressed by bzip2, use -t option
 $ bzip2 -t [compressed-file-name]
If the file is a valid file then no output will be returned but if the file is not valid you will receive error messages. We can also add -v option at here, then for valid files it returns OK message.
 $ bzip2 -tv [compressed-file-name]
7. Use bzip2 with Less Memory When Compressing Files :

When compressing files in less memory powered system, then we can use -s option to reduce the memory use by bzip2, but it takes longer to compress a file using this switch. 
 $ bzip2 -s document.dat
8. To recover broken files :

If the compressed file is broken, then we will try to recover it by bzip2recover utility :
 $ bzip2recover [compressed-file-name]
9. There is also an alternative program bunzip2 which is used to decompress programs instead of 'bzip2 -d ' option :
 $ bunzip2 [compressed-file-name]
example :
 $ bunzip2 document.dat.bz2
At last for manual page type :
 $ man bzip2

Conclusion : 

The bzip2 is very powerful tool to compress and/or decompress files. In this post we saw some basic usage of bzip2 command with examples. For more information about bzip2 you can check mannual page at here :