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:
| Option | Meaning |
|---|---|
| c | Create a new archive |
| x | Extract an archive |
| t | List the contents |
| v | Show progress (verbose) |
| f | Name of the archive file |
| z | Use gzip compression |
| j | Use bzip2 compression |
| C | Change 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.gzThis 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/restoreThe -C option sends the files into a chosen folder. Create that folder first.
Extract One File
tar -xzvf backup.tar.gz documents/notes.txtAdd 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 fileZip 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.zipInstall the tools with sudo apt install zip unzip on Ubuntu.
Check Sizes Before and After
du -sh documents/
ls -lh backup.tar.gzText 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 -deleteThe 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.sha256The second command prints OK when the file matches the stored fingerprint.
Choose a Format
| Format | Best For |
|---|---|
| .tar.gz | Fast everyday backups on Linux and macOS |
| .tar.bz2 | Smaller files when time is not important |
| .zip | Sharing with Windows users |
| .gz | Single log files |
Key Takeaways
tar -czvfcreates a compressed archive.tar -xzvfextracts it andtar -tvflists it.gzipcompresses single files.zipandunzipsuit sharing with other systems.- Add the date to archive names for easy tracking.
