Compression & Archiving – Linux Teaching Plan

Duration: 2 hours (≈ 12 min per command). Audience: System administrators, DevOps engineers, and developers who routinely package or backup data on Linux.

Learning Objectives

  1. Compress and decompress files with gzip, bzip2, xz, lz4, zstd, and pigz.
  2. Create, list, and extract archives using tar, zip, 7z, and ar.
  3. Build multi‑volume archives and split archives for transport.
  4. Automate backup pipelines with a Bash script that uses the above tools.
  5. Understand trade‑offs between compression ratio, speed, and resource usage.

Schedule & Topics

  1. Introduction to compression – 5 min
  2. gzip – 10 min
  3. bzip2 – 10 min
  4. xz – 10 min
  5. lz4 – 10 min
  6. zstd – 10 min
  7. pigz – 10 min
  8. tar – 10 min
  9. zip/unzip – 10 min
  10. 7z – 10 min
  11. ar (static libs) – 10 min
  12. Multi‑volume & split archives – 10 min
  13. Mini‑project: Backup & restore script – 20 min
  14. Assessment & Wrap‑up – 10 min

Command Topics & Hands‑On Exercises

1. gzip – Lossless File Compression

# Compress a single file
gzip -c largefile.txt > largefile.txt.gz

# Decompress
gzip -d -c largefile.txt.gz > largefile-decompressed.txt

# Show compression ratio
gzip -l largefile.txt.gz

2. bzip2 – High Compression Ratio

# Compress
bzip2 -k myfile.txt       # -k keeps the original

# Decompress
bunzip2 myfile.txt.bz2

# View info
bzip2 -l myfile.txt.bz2

3. xz – Very High Compression

# Compress
xz -z -k -9 file.txt

# Decompress
xz -d -k file.txt.xz

# Show stats
xz -l file.txt.xz

4. lz4 – Speed over Ratio

# Compress
lz4 -k file.txt

# Decompress
lz4 -d file.txt.lz4 -o file-decompressed.txt

# Benchmark
time lz4 -k largefile.txt
time gzip -k largefile.txt

5. zstd – Balanced Speed & Ratio

# Compress
zstd -k -19 file.txt

# Decompress
unzstd -k file.txt.zst

# Inspect
zstd --list file.txt.zst

6. pigz – Parallel Gzip

# Compress using all CPUs
pigz -k file.txt

# Decompress
pigz -d -k file.txt.gz

# Compare performance
time pigz -k file.txt
time gzip -k file.txt

7. tar – Archive Multiple Files

# Create a gzip archive
tar -czvf project.tar.gz project/

# Create an xz archive
tar -cJvf project.tar.xz project/

# List contents
tar -tvf project.tar.gz

# Extract specific file
tar -xvf project.tar.gz path/to/file.txt

8. zip / unzip – Portable Archiving

# Recursive zip
zip -r archive.zip src/ docs/

# Add files
zip archive.zip readme.md

# List
zipinfo archive.zip

# Extract
unzip archive.zip -d extracted/

9. 7z – High Compression & AES‑256

# Create archive with 7z
7z a -tzip -mx=9 archive.zip folder/

# Add encrypted file
7z a -pMySecret -mem=AES256 secure.7z secret.txt

# List contents
7z l archive.zip

# Extract
7z x archive.zip -oextracted/

10. ar – Archive Static Libraries

# Create an object file
gcc -c foo.c -o foo.o

# Build a static lib
ar rcs libfoo.a foo.o

# List archive contents
ar -t libfoo.a

# Extract a file
ar -x libfoo.a foo.o

11. split / cat – Multi‑volume & Splitting

# Split a large file into 50 MB pieces
split -b 50M largefile.bin part_

# Reassemble
cat part_* > largefile-assembled.bin

# Split an archive
tar -cvf - data/ | split -b 200M - data.tar.part_

# Reconstruct & extract
cat data.tar.part.* | tar -xvf -

Mini‑Project: Incremental Backup & Restore

Students will write a Bash script backup.sh that:

  1. Creates a timestamped directory backups/YYYYMMDD-HHMMSS.
  2. Uses tar with -I pigz -c to archive /var/www and /etc, storing the archive as www.tgz and etc.tgz.
  3. Creates a .gz archive for each log file in /var/log using gzip -9.
  4. Compresses the backup directory with 7z a -tzip -mx=9 to produce a single backup.zip.
  5. Provides a restore.sh that can extract the archive and restore files to their original locations.

Deliverables: backup.sh, restore.sh, README.md explaining the workflow, and a video demo.

Assessment & Wrap‑up

  1. Quiz (5 questions) – 3‑choice MC covering gzip vs bzip2 vs xz vs lz4 vs zstd.
  2. Hands‑on test – Run a command chain that compresses /etc/nginx with tar -cJvf, splits it into 100 MB pieces, and later reassembles it.
  3. Project Demo – Students present their backup script, explain compression choices, and show a successful restore.

Score sheet (out of 35 points):

TaskPoints
gzip benchmark 5
bzip2 vs xz ratio comparison 5
tar archive with pigz 5
zip/unzip with filtering 5
7z encrypted archive 5
ar static lib 5
Split & reassemble 5
Backup/restore script 5

Resources