The Linux tar Command: Wrapping My Head Around Archives
The Linux tar Command: Wrapping My Head Around Archives
I needed to send a 500MB codebase to a client. "Just use tar," my boss said. Six hours later, after three failed uploads and a corrupted archive, I finally understood tar—through making every mistake possible.
Let me save you those six hours.
My tar Disaster
First, I tried creating a simple archive:
tar -cf site.tar /var/www/html
Seemed to work. But when the client tried to extract, they got warnings. Turns out I needed to compress!
Compression flags:
-z= gzip (.tar.gz or .tgz)-j= bzip2 (.tar.bz2)-J= xz (.tar.xz)
Correct command:
tar -czf site.tar.gz /var/www/html
Understanding tar Options
Create Archives
tar -czf archive.tar.gz directory/
tar -cjf archive.tar.bz2 directory/
tar -cJf archive.tar.xz directory/
The -c = create. Always put it first!
Extract Archives
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2
tar -xJf archive.tar.xz
The -x = extract.
List Contents
tar -tzf archive.tar.gz
See what's inside without extracting.
Common Flags (can be combined)
-v= verbose (show files)-p= preserve permissions-m= preserve modification time--strip-components=N= remove N directory levels
The tar Mistakes That Cost Me Time
1. Trying to extract a file instead of an archive
You can't extract with create flag!
tar: Refusing to read archive body is disabled because -i option is not set
Fix: Use -x (extract), not -c (create).
2. Forgetting compression
Uncompressed tar files are huge:
tar -cf site.tar site/
# Results in: site.tar (2GB)
tar -czf site.tar.gz site/
# Results in: site.tar.gz (200MB)
3. Absolute paths
Extracting an absolute path puts files in /, not your current directory:
tar -xzf archive.tar.gz # Extracts to /var/www if that's in archive
tar -xzf archive.tar.gz -C /tmp # Extracts to /tmp instead
4. Not preserving permissions
Need -p to keep file modes:
tar -xpf archive.tar.gz
tar Commands That Actually Work
Creating a compressed archive
tar -czvf site-backup.tar.gz /var/www/html
Create, verbose, gzip, preserve perms.
Extracting to a specific directory
tar -xzf backup.tar.gz -C /tmp
Extract to /tmp.
Listing with details
tar -tvf archive.tar.gz
Verbose list.
Adding files to existing archive
tar -rf archive.tar newfile.txt
But only works for uncompressed!
Updating files in archive
tar -uf archive.tar updated-file.txt
Like adding but smarter.
Excluding files
tar -czf site.tar.gz --exclude='.git' --exclude='node_modules' site/
Skip unnecessary directories.
Including only certain files
tar -czf docs.tar.gz --include='*.md' --include='*.txt' project/
Only documentation files.
The tar Command Builder: Visual Archiving
Building tar commands is tricky—there are many flags and order matters. The tar Command Builder helps:
- Click to create or extract mode
- Select compression type visually
- Add excludes with checkboxes
- See the command build in real-time
Point, click, extract.
Quick Reference
| Task | Command |
|---|---|
| Create tar | tar -cf out.tar dir/ |
| Create gzip | tar -czf out.tar.gz dir/ |
| Create bzip2 | tar -cjf out.tar.bz2 dir/ |
| Extract | tar -xf archive.tar.gz |
| List contents | tar -tf archive.tar.gz |
| Extract to dir | tar -xf -C /tmp |
| Verbose create | tar -czvf out.tar.gz dir/ |
| Exclude | tar --exclude='.git' -czf out.tar.gz dir/ |
Advanced tar Techniques
Incremental backup
tar -czvf backup.tar.gz --listed-incremental=backup.snar /var/www
First run creates full. Subsequent runs are incrementals!
Remote transfer
tar -czf - /var/www | ssh user@server "tar -xzf - -C /backup"
Archive locally, transfer through SSH, extract remotely.
Split archive
tar -czf - bigdir/ | split -b 100M - backup.tar.gz.
Split into 100MB chunks for CD/DVD.
Lessons from tar Disasters
-
Use -z (gzip) by default — compression saves bandwidth and time.
-
Use -v (verbose) when learning — see each file being processed.
-
Use -p (preserve) always — protects permissions and timestamps.
-
Use -C to specify extraction directory — avoids accidental wrong location.
-
Check contents first with -t — see what's inside before extracting.
Conclusion: tar Is Your Friend
tar became my nightmare, then my ally. The key is understanding the create/extract distinction and using compression.
The tar Command Builder makes building commands easy—select what you need, copy the result.
