Chapter 44: Bash TAR Archive
Bash TAR Archive
tar is one of the oldest and most important commands in the entire Unix/Linux/Bash world. It’s been around since the 1970s (yes, really!) and is still used every single day in 2026 by developers, sysadmins, cloud engineers, and everyone who touches servers.
tar stands for Tape ARchiver (Back in the old days, people saved backups on magnetic tapes — tar was made to write/read those tapes.)
In modern life, tar does two main jobs:
- Bundles many files and folders into one single file (called an archive) → Like putting 50 books into one big cardboard box so you can carry them easily.
- Preserves all Unix/Linux file information perfectly: permissions, timestamps, ownership, symbolic links, special files, etc.
Important point: tar alone does NOT compress anything — it just bundles. The file usually gets bigger (because of extra metadata headers). To make it smaller, we almost always combine tar with a compressor: gzip, xz, zstd, bzip2 → creating the famous formats:
- .tar.gz or .tgz (tar + gzip)
- .tar.xz (tar + xz)
- .tar.zst (tar + zstd)
- .tar.bz2 (tar + bzip2)
- plain .tar (no compression)
Why is tar so special in 2026?
- It preserves Unix file attributes better than zip
- Almost every Linux software package, source code download, backup script, Docker image layer, backup tool uses tar
- It’s extremely reliable — you can extract a 20-year-old .tar.gz today and it still works perfectly
- It’s the standard for server backups, website deploys, config bundles, etc.
Basic tar Commands – The Ones You’ll Use 95% of the Time
1. Create an Archive (No Compression)
|
0 1 2 3 4 5 6 |
tar -cvf myfiles.tar folder1/ file2.txt photo.jpg |
- -c = create new archive
- -v = verbose (show every file being added — very useful)
- -f = file name follows (the archive name)
2. Create + Compress in One Step (Most Common!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# gzip (fast & very compatible) tar -czvf backup.tar.gz my_project/ # xz (best compression ratio, but slower) tar -cJvf super_small.tar.xz big_database/ # zstd (fast + excellent compression – modern favorite in 2026) tar -caf fast_good.tar.zst important_files/ # -a = auto detect compressor # or explicitly: tar --use-compress-program=zstd -cvf archive.tar.zst folder/ |
Common shortcuts people remember:
| Format | Command Shortcut | Full Command Example | When to use |
|---|---|---|---|
| .tar.gz | -z | tar -czvf archive.tar.gz folder/ | Everyday, fast, everywhere |
| .tar.xz | -J | tar -cJvf archive.tar.xz folder/ | Maximum size saving |
| .tar.zst | -a or –zstd | tar -caf archive.tar.zst folder/ | Best speed + good ratio (2026) |
| .tar.bz2 | -j | tar -cjvf archive.tar.bz2 folder/ | Older systems |
| plain .tar | (no letter) | tar -cvf archive.tar folder/ | When you don’t want compression |
Pro tip: Always add trailing / after folder name when creating → means “contents of folder”, not the folder wrapper itself.
3. List Contents Without Extracting (-t)
Very useful — like peeking inside without unpacking.
|
0 1 2 3 4 5 6 7 8 |
tar -tvf backup.tar.gz # or tar -tf archive.tar.xz # -t = list, no v for quiet |
Shows file names, permissions, sizes, dates — same as ls -l
4. Extract an Archive (-x)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Extract everything tar -xzvf backup.tar.gz # Extract to specific folder tar -xzvf backup.tar.gz -C ~/extract_here/ # Extract only one file or folder tar -xzvf archive.tar.gz "my_project/src/main.py" tar -xzvf archive.tar.gz "docs/" |
Common extract shortcuts:
| Format | Extract Command | Notes |
|---|---|---|
| .tar.gz | tar -xzvf file.tar.gz | Most common |
| .tar.xz | tar -xJvf file.tar.xz | Capital J |
| .tar.zst | tar -xaf file.tar.zst | -a = auto detect |
| .tar | tar -xvf file.tar | No compression letter |
-C = change to directory before extracting (very clean!)
5. Quick Practice Session Right Now (Do This!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 1. Make test folder mkdir -p tar_practice/photos tar_practice/docs cd tar_practice echo "Report 2026" > docs/report.txt touch photos/pic{1..3}.jpg # 2. Create compressed archive tar -czvf my_backup.tar.gz docs/ photos/ # 3. List contents tar -tvf my_backup.tar.gz # 4. Extract somewhere else mkdir ../extracted tar -xzvf my_backup.tar.gz -C ../extracted/ # 5. Check ls -R ../extracted/ # Bonus: zstd version (if installed) tar -caf modern_backup.tar.zst . |
Summary Table – tar Commands Cheat Sheet
| Goal | Command Example | Shortcut Letter |
|---|---|---|
| Create plain tar | tar -cvf archive.tar folder/ | none |
| Create .tar.gz | tar -czvf archive.tar.gz folder/ | -z (gzip) |
| Create .tar.xz | tar -cJvf archive.tar.xz folder/ | -J (xz) |
| Create .tar.zst | tar -caf archive.tar.zst folder/ | -a (auto) |
| List contents | tar -tvf archive.tar.gz | -t |
| Extract everything | tar -xzvf archive.tar.gz | -x |
| Extract to folder | tar -xzvf archive.tar.gz -C ~/dest/ | -C |
| Extract one file | tar -xzvf archive.tar.gz path/inside/file.txt | — |
Teacher’s Golden Rules
- Always use -v when learning — you see exactly what’s happening
- Always use -C when extracting — keeps your current folder clean
- Use .tar.zst or .tar.xz for best size savings in 2026
- Use .tar.gz when you want maximum compatibility
- Never compress already-compressed files (jpg, mp4, zip inside) — waste of time
- For backups: add date → backup_$(date +%Y-%m-%d).tar.gz
Got it, boss? tar is the heart of Linux archiving — once you master create/list/extract, you can handle 99% of downloaded archives, backups, and source code packages.
Any part confusing? Want next: “Teacher, explain how to add files to existing tar” or “tar vs zip deep comparison” or “tar in backup scripts with cron”?
Just say — teacher is ready in Hyderabad! Keep archiving like a pro! 🐧📦📼 😄
