Why we Compress Files and the bzip2 Utility

by
Share this Article: Facebook0Google+0Twitter10LinkedIn0Reddit0StumbleUpon0

You may frequently download compressed files from the internet. Have you wondered why the files you download are compressed?

Not everyone has the privilege to pay for a fast internet connection and not everyone can afford an external hard drive. Transferring files through slow internet connection is boring and time consuming. Can we find a solution for this? Yes we can! We always can! Do you send large documents via email? Do you compress your files before sending them to your boss? No? Ok, don’t do that anymore because this costs the recipient time, energy and money. Now it is time to learn how to compress files using the command-line. Don’t worry guys, it is not going to be hard, I promise.

The utility you will learn in this article compresses and decompresses files. After you have downloaded a compressed file from the internet you need to decompress it so you can use it.

The bzip2 Utility

The bzip2 tool compresses files and it works very well on text files. This utility analyses the files which is going to be compressed and records it more efficiently. You can say from the way it works that this utility works particular well on files that have repeated information such as image data and text data. How do I know if a files is compressed with the bzip2 utility or not? The bzip2 utility renames the files it compresses by adding the bz2 suffix. Note: The compressed file can not be displayed or used without being decompressed. Ok, now it is time to show you guys the bzip2 utility in action.

I have an image on my desktop and I want to compress it using the bzip2 utility. You can see from Figure 1 that the size of my image is 24K and it is in PNG format.

bzip1

Figure 1

To compress my image I use ‘bzip2 -v file_name‘. The -v options stands for verbose and I use it because I want to know what is happening. The -v option gives you information about the input size, output size and the percentage of compression. You can see that in details by analysing Figure 2.

bzip2

Figure 2

As you can see from Figure 2, the bzip2 utility can reduce the size of our files by 5.12%. If you are the curious type of guy you can calculate that manually and verify that the percentage is correct. I did. Now, you can see from Figure 3 that in place of the comparison.png file is comparison.png.bz2 file. Does bz2 tell anything to you? What information do you get from this suffix? Can you open the file by double clicking it like you did with the image file? Let me give a try. I will be back…

Guys I am back and this is what I get when I double click the comparison.png.bz2 file.

bz3

Figure 3

I can view and extract my file but I can not use it without extracting it. I prefer to do my things in the command-line, so I will extract my image using the bzip2 utility. Type the ‘bunzip2 file_name‘ like shown in Figure 4 to decompress the file you compressed with the bzip2 utility. The bunzip2 utitlity is used to decompress a file that has been compressed with the bzip2 utility.

bzip4

Figure 4

If I run the ‘ls‘ command the comparison.png file will show up.

bz5

Figure 5

Conclusion

We went through a simple example to learn the importance of file compression. We compress files to transfer them through slow internet connections, to attach large files in emails and to save space in general.

Did you know that the Linux man pages are compressed with the gzip utility? If not, you know now. Linux stores manual pages in gzip format to save disk space.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/


Share this Article: Facebook0Google+0Twitter10LinkedIn0Reddit0StumbleUpon0