Archive Management with tar
When you need to back up a directory, transfer a collection of files, or preserve file permissions and ownership during a move, tar is the tool. Despite its reputation for cryptic flags, tar uses the same four or five options for 90% of real-world tasks. This article explains those options clearly so you never have to guess.
How tar works
The name tar stands for Tape ARchive. It was originally designed for magnetic tape backups. Today it packs multiple files and directories into a single archive file while preserving their structure, permissions, ownership, and timestamps. Compression (gzip, bzip2, xz) can be added on top.
Multiple files Single archive
/etc/nginx/
├── nginx.conf ──────────► etc-backup.tar.gz
├── sites-available/ (compressed, single file)
│ └── mysite (preserves permissions)
└── sites-enabled/ (preserves ownership)
└── mysite -> ... (preserves symlinks)The flags that matter: c=create, x=extract, t=list, v=verbose, f=file, z=gzip, j=bzip2, J=xz.
Creating archives
# Create a gzip-compressed archive of a directory
# -c create, -z gzip, -f filename, -v verbose (shows files added)
tar -czvf backup.tar.gz /etc/nginx/
# Create without verbose output (for scripts)
tar -czf backup.tar.gz /etc/nginx/
# Create a timestamped archive (useful for scheduled backups)
tar -czf "/backup/nginx-$(date +%Y-%m-%d).tar.gz" /etc/nginx/
# Archive multiple directories
tar -czf config-backup.tar.gz /etc/nginx/ /etc/mysql/ /etc/php/
# Create a bzip2 archive (better compression, slower)
tar -cjf backup.tar.bz2 /etc/nginx/
# Create an xz archive (best compression, slowest)
tar -cJf backup.tar.xz /etc/nginx/
# Archive and preserve ownership/permissions (add -p for non-root)
tar -czpf backup.tar.gz /home/irfan/
Example verbose output
tar: Removing leading `/' from member names
/etc/nginx/
/etc/nginx/nginx.conf
/etc/nginx/sites-available/
/etc/nginx/sites-available/default
/etc/nginx/sites-enabled/
/etc/nginx/sites-enabled/default -> ../sites-available/default
📝 NOTE: The message "Removing leading
/from member names" is not an error. It is tar safely converting absolute paths to relative paths inside the archive, so extracting it does not force files to/etc/nginx— you can extract to any directory.
Listing archive contents
# List contents of an archive without extracting
tar -tzvf backup.tar.gz
# List contents of a bzip2 archive
tar -tjvf backup.tar.bz2
# Search for a specific file in an archive
tar -tzvf backup.tar.gz | grep nginx.conf
Output
drwxr-xr-x root/root 0 2026-06-10 09:00 etc/nginx/
-rw-r--r-- root/root 1823 2026-06-10 09:00 etc/nginx/nginx.conf
drwxr-xr-x root/root 0 2026-06-10 09:00 etc/nginx/sites-available/
Always list the archive before extracting on an unfamiliar archive. Some archives from the internet extract everything into the current directory without a subdirectory, creating a mess of files. Others extract correctly into their own folder.
Extracting archives
# Extract into the current directory
tar -xzvf backup.tar.gz
# Extract to a specific directory (recommended — prevents messy extracts)
tar -xzvf backup.tar.gz -C /tmp/restore/
# Extract a specific file from the archive
tar -xzvf backup.tar.gz etc/nginx/nginx.conf
# Extract a specific directory from the archive
tar -xzvf backup.tar.gz etc/nginx/sites-available/
# Extract and strip the leading directory level
tar -xzvf backup.tar.gz --strip-components=1
# Extract without overwriting existing files
tar -xkvf backup.tar.gz
💡 TIP: Use
-C /tmp/restore/when extracting archives you are not familiar with. It keeps the extraction contained to a specific directory so you can review the contents before moving files to their final location.
Compression options compared
| Format | Extension | Flag | Speed | Compression | When to use |
|---|---|---|---|---|---|
| gzip | .tar.gz or .tgz | -z | Fast | Good | Most situations; default choice |
| bzip2 | .tar.bz2 | -j | Slow | Better | When space matters more than time |
| xz | .tar.xz | -J | Very slow | Best | Distribution archives, maximum compression |
| none | .tar | (no flag) | Fastest | None | When you are compressing separately or on SSD |
# Compare compression ratios for the same directory
tar -czf /tmp/test.tar.gz /etc/nginx/
tar -cjf /tmp/test.tar.bz2 /etc/nginx/
tar -cJf /tmp/test.tar.xz /etc/nginx/
ls -lh /tmp/test.tar.*
Practical backup patterns
# Daily config backup with date in filename
BACKUP_DIR="/backup"
DATE=$(date +%Y-%m-%d)
tar -czf "$BACKUP_DIR/etc-$DATE.tar.gz" /etc/
# Backup MySQL data directory (stop MySQL first for consistency)
sudo systemctl stop mysql
sudo tar -czf "/backup/mysql-$DATE.tar.gz" /var/lib/mysql/
sudo systemctl start mysql
# Verify the backup was created and is not empty
ls -lh "/backup/etc-$DATE.tar.gz"
tar -tzf "/backup/etc-$DATE.tar.gz" | wc -l # Count files in archive
# Delete archives older than 7 days
find /backup/ -name "*.tar.gz" -mtime +7 -delete
Common tar mistakes
- Forgetting to check the archive structure before extracting: Run
tar -tzvf archive.tar.gzfirst. If all files are at the root level (no subdirectory), extracting in your home directory will create dozens of files scattered around. - Archiving a running database: Databases write continuously. Archiving the data directory while the database is running produces a corrupt, unusable backup. Always stop the service or use the database’s own dump tool (
mysqldump,pg_dump). - Using relative paths and extracting to the wrong directory: Always specify
-C /destination/explicitly rather than relying on your current directory. - Not testing the backup: A backup you cannot restore is not a backup. After creating an important archive, extract it to
/tmpand verify the files are readable.
Conclusion
Five tar operations cover almost everything: create (-czf), list (-tzf), extract (-xzf), extract to specific directory (-xzf -C /dest/), and extract a specific file (-xzf archive.tar.gz path/to/file). Always list an unfamiliar archive before extracting, always test backup archives by extracting them, and never archive a running database without a proper dump first.
FAQ
Is Archive Management with tar important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support