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
  1. Connect to the lab host and become root

    This exercise continues from the uncompressed /tmp/etc-backup.tar created in the previous exercise. Confirm it is still there.

    ssh student@servera
    sudo -i
    ls -lh /tmp/etc-backup.tar
  2. Compress with gzip

    Add -z to the options you already know. tar will 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
  3. Compress with bzip2

    bzip2 is 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
  4. Compress with xz

    xz produces 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
  5. 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*
  6. Ask gzip and xz for their ratios

    Both tools report the ratio without unpacking anything. bzip2 has no such option.

    gzip -l /tmp/etc-backup.tar.gz
    xz -l /tmp/etc-backup.tar.xz
  7. Extract without naming the algorithm

    tar inspects the file and picks the right decompressor itself. No -J is needed.

    mkdir -p /tmp/etc-xz-extract
    cd /tmp/etc-xz-extract
    tar -xf /tmp/etc-backup.tar.xz
    ls
  8. Clean up

    Remove the archives and the extracted directories, remove the bzip2 package 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