Exercise
Comparing Compression Algorithms Exercise
Overview
Compression buys smaller files and pays for them in CPU time. How good the trade is depends on which algorithm you choose.
In this hands-on exercise you will build the same
archive three more times, once each with
gzip, bzip2, and
xz, timing every run. Then you will compare
the four results side by side and confirm that
extracting a compressed archive requires no extra option
at all.
- Host
servera- Run as
root- Prerequisite
- Creating and Extracting Archives Exercise
-
Connect to the lab host and become root
This exercise continues from the uncompressed
/tmp/etc-backup.tarcreated in the previous exercise. Confirm it is still there.ssh student@servera sudo -i ls -lh /tmp/etc-backup.tar -
Compress with gzip
Add
-zto the options you already know.tarwill not add the file extension for you, so name the file for what it holds.dnf info gzip time tar -czvf /tmp/etc-backup.tar.gz /etc -
Compress with bzip2
bzip2is not part of a minimal installation, so install it before you reach for it.dnf info bzip2 dnf install -y bzip2 time tar -cjvf /tmp/etc-backup.tar.bz2 /etc -
Compress with xz
xzproduces the smallest archive of the three and takes the longest to do it.dnf info xz time tar -cJvf /tmp/etc-backup.tar.xz /etc -
Compare all four archives
Sort by size so the difference is easy to read, then set the sizes beside the times you noted.
ls -lh --sort=size /tmp/etc-backup.tar* -
Ask gzip and xz for their ratios
Both tools report the ratio without unpacking anything.
bzip2has no such option.gzip -l /tmp/etc-backup.tar.gz xz -l /tmp/etc-backup.tar.xz -
Extract without naming the algorithm
tarinspects the file and picks the right decompressor itself. No-Jis needed.mkdir -p /tmp/etc-xz-extract cd /tmp/etc-xz-extract tar -xf /tmp/etc-backup.tar.xz ls -
Clean up
Remove the archives and the extracted directories, remove the
bzip2package you installed, then leave the root shell.cd /tmp rm -rf /tmp/etc-backup.tar* /tmp/etc-extract /tmp/etc-one /tmp/etc-xz-extract dnf remove -y bzip2 exit