Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.

LPI Linux Essentials Exam 010-160 - Topic 3.1 - Compression with bzip2

bzip2 Overview

bzip2 is a file compression and decompression tool used in Linux systems. The bzip2 command reduces the size of the named files using Burrows-Wheeler block sorting text compression algorithm and Huffman coding. bzip2 replaces the original file with a compressed version that has a .bz2 extension. Compressed files can be significantly smaller in size, saving storage space and reducing file transfer times.

Create a Practice File

To create a file for practicing bzip2 with in up coming exercises, follow the below:

Exercise: Creating files for practice with the bzip2 command:

journalctl > bzip2-test-default.txt
ls -lR /etc /usr/bin /usr/share/doc >> bzip2-test-default.txt 2> /dev/null
cp bzip2-test-default.txt bzip2-test-fast.txt
cp bzip2-test-default.txt bzip2-test-best.txt

Note down the original size of the bzip2-test-default.txt file as this will be used to compare file sizes post compression in later exercises.

ls -lh bzip2-test-default.txt

59M in my case.

-rw-rw-r-- 1 dtvlinux dtvlinux 59M Oct 22 15:10 bzip2-test-default.txt

Compression Using bzip2

When using bzip2, you can use different flags to regulate the speed of compression (-1 to -9). Where -1 indicates the fastest compression method (less compression) and -9 indicates the slowest compression method (best compression). The default compression level is -9 (which is biased towards high compression at the expense of speed).

Using the -v flag will show the compression percentage as well as the new file name.

Exercise: Using bzip2 with default compression:

bzip2 -v bzip2-test-default.txt

Exercise: Using bzip2 with fastest compression:

bzip2 -v -1 bzip2-test-fast.txt

Exercise: Using bzip2 with the best compression:

bzip2 -v -9 bzip2-test-best.txt

Exercise: Comparing compressed file sizes:

ls -lh bzip2-test-*.txt.bz2

In my case, we can see that -9 has been confirmed as the being the default option with file sizes matching, both of which have a significant improvement over the -1 option.

-rw-rw-r-- 1 dtvlinux dtvlinux 4.2M Oct 22 15:10 bzip2-test-best.txt.bz2
-rw-rw-r-- 1 dtvlinux dtvlinux 4.2M Oct 22 15:10 bzip2-test-default.txt.bz2
-rw-rw-r-- 1 dtvlinux dtvlinux 7.7M Oct 22 15:10 bzip2-test-fast.txt.bz2

Viewing Compressed Data Using bzcat

bzcat uncompresses either a list of files on the command line and writes the uncompressed data to standard output (typically the screen). This is useful if you want to view the file but you want it to remain compressed afterwards.

Exercise: View the contents of a compressed file using bzcat:

bzcat bzip2-test-default.txt.bz2

Without using a pager such as less or more the whole file will be displayed but too quick to read. Therefore run the command again, but pipe the output through the less command:

bzcat bzip2-test-default.txt.bz2 | less

Now you can press Space to page through the file. These practice files may be fairly large so you may wish to quit out of the pager by pressing Q.

Decompression Using bzip2/bunzip2

There are two ways to decompress files that were compressed using bzip2. You can either use bzip2 itself with the -d or --decompress option, or you can use the bunzip2 command.

Using the -v flag will show the compression percentage.

Exercise: Using bzip2 -d to decompress a file:

bzip2 -v -d bzip2-test-default.txt.bz2

Exercise: Using bzip2 --decompress to decompress a file:

bzip2 -v --decompress bzip2-test-best.txt.bz2

Exercise: Using bunzip2 to decompress a file:

bunzip2 -v bzip2-test-fast.txt.bz2

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.