Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Understand and Use Essential Tools: Compression & Decompression Using 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, run:
Creating files with data using output from the dmesg
& journalctl
commands:
dmesg > bzip2-test-default.txt
journalctl >> bzip2-test-default.txt
ls -lR / >> 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
24M in my case.
-rw-r--r--. 1 user1 user1 24M Jun 27 07:44 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.
Using bzip2
with default compression:
bzip2 -v bzip2-test-default.txt
Using bzip2
with fastest compression:
bzip2 -v -1 bzip2-test-fast.txt
Using bzip2
with the best compression:
bzip2 -v -9 bzip2-test-best.txt
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-r--r--. 1 user1 user1 1.4M Jun 27 07:44 bzip2-test-best.txt.bz2
-rw-r--r--. 1 user1 user1 1.4M Jun 27 07:44 bzip2-test-default.txt.bz2
-rw-r--r--. 1 user1 user1 1.8M Jun 27 07:44 bzip2-test-fast.txt.bz2
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.
Using bzip2 -d
to decompress a file:
bzip2 -v -d bzip2-test-default.txt.bz2
Using bzip2 --decompress
to decompress a file:
bzip2 -v --decompress bzip2-test-best.txt.bz2
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.