Bash Archives

An archive bundles many files into one file. Compression shrinks the bundle to save space and speed up transfers. The tar command creates bundles, and tools such as gzip and zip compress them.

Suitcase Analogy

Packing for a trip means placing many items into one suitcase. A vacuum bag squeezes the clothes smaller. Tar acts as the suitcase, and compression acts as the vacuum bag.

  file1  file2  file3  folder/
     \     |     /      /
      +----+----+-------+
           |
           v  tar
      archive.tar          (one file, same size)
           |
           v  gzip
      archive.tar.gz       (one file, smaller)

Create a Tar Archive

tar -cvf backup.tar documents/

Each letter in the options has a role:

OptionMeaning
cCreate a new archive
xExtract an archive
tList the contents
vShow progress (verbose)
fName of the archive file
zUse gzip compression
jUse bzip2 compression
CChange to a folder before working

Create a Compressed Archive

tar -czvf backup.tar.gz documents/
tar -cjvf backup.tar.bz2 documents/

The gzip form runs fast. The bzip2 form shrinks files more but needs more time.

List the Contents

tar -tvf backup.tar.gz

This command shows file names without extracting them.

Extract an Archive

tar -xvf backup.tar
tar -xzvf backup.tar.gz
tar -xzvf backup.tar.gz -C /tmp/restore

The -C option sends the files into a chosen folder. Create that folder first.

Extract One File

tar -xzvf backup.tar.gz documents/notes.txt

Add Files and Exclude Files

tar -rvf backup.tar newfile.txt
tar -czvf site.tar.gz website/ --exclude="*.log"

The -r option appends to an uncompressed archive. The --exclude option skips matching files.

Compress a Single File

gzip report.txt            # creates report.txt.gz and removes the original
gzip -k report.txt         # keeps the original
gunzip report.txt.gz       # restores the file

Zip Files

The zip format opens easily on Windows and macOS.

zip -r photos.zip photos/
unzip photos.zip
unzip photos.zip -d /tmp/photos
unzip -l photos.zip

Install the tools with sudo apt install zip unzip on Ubuntu.

Check Sizes Before and After

du -sh documents/
ls -lh backup.tar.gz

Text files shrink a lot. Photos, videos, and files that are already compressed shrink very little.

Practical Example: Daily Backup Script

#!/bin/bash
backup_dir="$HOME/backups"
stamp=$(date +%Y%m%d)

mkdir -p "$backup_dir"
tar -czf "$backup_dir/docs_$stamp.tar.gz" -C "$HOME" documents

echo "Backup saved: $backup_dir/docs_$stamp.tar.gz"
find "$backup_dir" -name "docs_*.tar.gz" -mtime +14 -delete

The script creates a dated archive and removes archives older than fourteen days.

Verify an Archive

A damaged archive wastes a backup. Test the file before trusting it. The tar -tzf command reads the whole archive and reports an error when the data is broken. The gzip -t command checks the compression layer alone.

tar -tzf backup.tar.gz > /dev/null && echo "Archive is healthy"
gzip -t report.txt.gz && echo "File is healthy"

Store a checksum next to important archives to detect changes later:

sha256sum backup.tar.gz > backup.tar.gz.sha256
sha256sum -c backup.tar.gz.sha256

The second command prints OK when the file matches the stored fingerprint.

Choose a Format

FormatBest For
.tar.gzFast everyday backups on Linux and macOS
.tar.bz2Smaller files when time is not important
.zipSharing with Windows users
.gzSingle log files

Key Takeaways

  • tar -czvf creates a compressed archive.
  • tar -xzvf extracts it and tar -tvf lists it.
  • gzip compresses single files.
  • zip and unzip suit sharing with other systems.
  • Add the date to archive names for easy tracking.