Lesson 15: Archives & Compression

Goal: Learn to create, extract, and manage archives and compressed files.


Table of Contents

  1. tar — The Tape Archiver
  2. gzip & gunzip
  3. bzip2 & xz
  4. zip & unzip
  5. Practical Scenarios
  6. Exercises

tar — The Tape Archiver

tar bundles files into a single archive. Combined with compression, it creates .tar.gz or .tar.bz2 files.

Key flags

Flag Meaning
-c Create archive
-x Extract archive
-t List contents
-v Verbose (show files)
-f File name (always last flag)
-z Compress with gzip (.tar.gz)
-j Compress with bjip2 (.tar.bz2)
-J Compress with xz (.tar.xz)
-C Extract to directory

Create archives

# Create a .tar.gz archive
tar -czf archive.tar.gz folder/

# Create with verbose output
tar -czvf backup.tar.gz /home/benjamin/Documents/

# Create .tar.bz2 (better compression, slower)
tar -cjvf archive.tar.bz2 folder/

# Create .tar.xz (best compression, slowest)
tar -cJvf archive.tar.xz folder/

# Archive multiple folders
tar -czf backup.tar.gz folder1/ folder2/ file.txt

# Archive with date in filename
tar -czf "backup-$(date +%Y%m%d).tar.gz" /home/benjamin/

Extract archives

# Extract .tar.gz
tar -xzf archive.tar.gz

# Extract with verbose
tar -xzvf archive.tar.gz

# Extract to a specific directory
tar -xzf archive.tar.gz -C /tmp/extracted/

# Extract .tar.bz2
tar -xjf archive.tar.bz2

# Extract .tar.xz
tar -xJf archive.tar.xz

List archive contents (without extracting)

tar -tzf archive.tar.gz
tar -tjf archive.tar.bz2

Extract specific files

# Extract only one file
tar -xzf archive.tar.gz path/to/file.txt

# Extract files matching a pattern
tar -xzf archive.tar.gz --wildcards "*.conf"

Exclude files

# Exclude node_modules and .git
tar -czf project.tar.gz project/ \
  --exclude='node_modules' \
  --exclude='.git' \
  --exclude='*.log'

gzip & gunzip

gzip compresses individual files (replaces the original).

# Compress a file
gzip largefile.txt
# Result: largefile.txt.gz (original is removed)

# Decompress
gunzip largefile.txt.gz
# or
gzip -d largefile.txt.gz

# Keep the original file
gzip -k largefile.txt

# View compressed file without extracting
zcat largefile.txt.gz
zless largefile.txt.gz
zgrep "pattern" largefile.txt.gz

# Compression levels (1=fast, 9=best)
gzip -9 largefile.txt    # best compression
gzip -1 largefile.txt    # fastest

bzip2 & xz

bzip2 — Better compression than gzip

bzip2 largefile.txt        # compress
bunzip2 largefile.txt.bz2  # decompress
bzip2 -k largefile.txt     # keep original

xz — Best compression ratio

xz largefile.txt        # compress
unxz largefile.txt.xz   # decompress
xz -k largefile.txt     # keep original

Compression comparison

Format Extension Speed Compression Best for
gzip .gz Fast Good Daily use, logs
bzip2 .bz2 Medium Better Large files
xz .xz Slow Best Distribution, archiving

zip & unzip

zip is the most universal format (works on Windows, macOS, Linux).

Create a zip

# Zip files
zip archive.zip file1.txt file2.txt

# Zip a directory recursively
zip -r archive.zip folder/

# Zip with compression level
zip -9 -r archive.zip folder/

# Add password protection
zip -e -r secret.zip folder/

# Exclude files
zip -r archive.zip folder/ -x "*.log" "*.tmp"

Extract a zip

# Extract to current directory
unzip archive.zip

# Extract to a specific directory
unzip archive.zip -d /tmp/extracted/

# List contents without extracting
unzip -l archive.zip

# Extract a specific file
unzip archive.zip path/to/file.txt

# Overwrite without asking
unzip -o archive.zip

Practical Scenarios

Backup your home directory

tar -czf "/tmp/home-backup-$(date +%Y%m%d).tar.gz" \
  --exclude='.cache' \
  --exclude='node_modules' \
  --exclude='.local/share/Trash' \
  /home/benjamin/

Transfer files between servers

# Create archive, transfer, extract in one pipeline
tar -czf - /home/benjamin/project/ | ssh user@server "tar -xzf - -C /home/user/"

Backup a database

# MySQL dump, compressed
mysqldump mydb | gzip > "mydb-$(date +%Y%m%d).sql.gz"

# Restore
gunzip < mydb-20260322.sql.gz | mysql mydb

Split large archives

# Create and split into 100 MB parts
tar -czf - largefolder/ | split -b 100M - archive.tar.gz.part_

# Reassemble and extract
cat archive.tar.gz.part_* | tar -xzf -

Exercises

Exercise 1: tar basics

# 1. Create test files
mkdir -p /tmp/tar-test/docs
echo "File 1" > /tmp/tar-test/file1.txt
echo "File 2" > /tmp/tar-test/file2.txt
echo "Doc" > /tmp/tar-test/docs/readme.md

# 2. Create a tar.gz archive
tar -czvf /tmp/test-archive.tar.gz -C /tmp tar-test/

# 3. List contents
tar -tzf /tmp/test-archive.tar.gz

# 4. Extract to a new location
mkdir /tmp/extracted
tar -xzf /tmp/test-archive.tar.gz -C /tmp/extracted/

# 5. Verify
ls -la /tmp/extracted/tar-test/

# 6. Clean up
rm -rf /tmp/tar-test /tmp/extracted /tmp/test-archive.tar.gz

Exercise 2: Compression comparison

# 1. Create a test file
dd if=/dev/urandom of=/tmp/testfile bs=1M count=10

# 2. Compare compression methods
cp /tmp/testfile /tmp/test-gz && time gzip /tmp/test-gz
cp /tmp/testfile /tmp/test-bz2 && time bzip2 /tmp/test-bz2
cp /tmp/testfile /tmp/test-xz && time xz /tmp/test-xz

# 3. Compare sizes
ls -lh /tmp/test-gz.gz /tmp/test-bz2.bz2 /tmp/test-xz.xz

# 4. Clean up
rm /tmp/testfile /tmp/test-gz.gz /tmp/test-bz2.bz2 /tmp/test-xz.xz

Key Takeaways

  • tar -czf archive.tar.gz folder/ — create gzip archive
  • tar -xzf archive.tar.gz — extract gzip archive
  • tar -tzf archive.tar.gz — list contents
  • gzip / bzip2 / xz — compress individual files
  • zip -r archive.zip folder/ — create zip (universal)
  • unzip archive.zip — extract zip
  • Use --exclude to skip unwanted files
  • gzip = fast, xz = smallest, zip = most compatible

Next Lesson: Lesson 16: Pipes, Redirection & Chaining →