tar Command in Linux â Archive Files with Compression Examples
In this tutorial, you'll learn about tar command in linux. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
tar (tape archive) is the Linux standard for Bundling files into a single archive â optionally with compression. Originally designed for tape drives, it's now the go-to tool for backups, software distribution, and file transfer.
What You'll Learn
By the end of this tutorial, you'll know how to create and extract archives, list contents, use gzip/bzip2/xz compression, exclude files, add files to existing archives, use verbose mode, and extract to specific directories.
Why tar Matters
Every Linux user deals with tar archives â downloading source tarballs (tar.gz), backing up directories, distributing application builds. DodaZIP is built on compression expertise; its core team uses tar daily for archiving log files, packaging builds, and creating backup snapshots.
tar Learning Path
flowchart LR A[find Command] --> B[tar Command
You are here] B --> C[rsync Command] C --> D[Backup Strategies] D --> E[System Administration] style B fill:#f90,color:#fff
Prerequisites: Basic Linux command-line skills. See essential Linux commands for fundamentals.
Syntax Overview
tar [options] [archive-file] [files/directories]
| Option | Description |
|---|---|
-c |
Create a new archive |
-x |
Extract files from archive |
-t |
List archive contents |
-f |
Specify archive filename |
-v |
Verbose (list files being processed) |
-z |
Filter through gzip (.tar.gz) |
-j |
Filter through bzip2 (.tar.bz2) |
-J |
Filter through xz (.tar.xz) |
--exclude |
Exclude files matching pattern |
-C DIR |
Change to directory before extracting |
-r |
Append files to existing archive |
-u |
Update â only add newer files |
10 Practical Examples
Create a sample directory to archive:
mkdir -p myproject/src myproject/docs myproject/tests
echo "print('hello')" > myproject/src/main.py
echo "# Documentation" > myproject/docs/guide.md
echo "def test_main(): pass" > myproject/tests/test_main.py
echo "*.log" > myproject/.gitignore
1. Create an Archive
Create a tar file (no compression):
tar -cf myproject.tar myproject
ls -lh myproject.tar
-rw-r--r-- 1 alice alice 10K Jun 20 10:00 myproject.tar
2. Extract an Archive
Extract to the current directory:
tar -xf myproject.tar
ls -d myproject
myproject
3. List Archive Contents
View contents without extracting:
tar -tf myproject.tar
myproject/
myproject/src/
myproject/docs/
myproject/tests/
myproject/.gitignore
myproject/src/main.py
myproject/docs/guide.md
myproject/tests/test_main.py
4. Create Compressed Archive with gzip
The most common format â .tar.gz:
tar -czf myproject.tar.gz myproject
ls -lh myproject.tar.gz
-rw-r--r-- 1 alice alice 312 Jun 20 10:00 myproject.tar.gz
Extract gzip archive:
tar -xzf myproject.tar.gz
5. Create with bzip2 Compression
Better compression ratio, slower:
tar -cjf myproject.tar.bz2 myproject
ls -lh myproject.tar.bz2
-rw-r--r-- 1 alice alice 268 Jun 20 10:00 myproject.tar.bz2
6. Create with xz Compression
Best compression ratio, slowest:
tar -cJf myproject.tar.xz myproject
ls -lh myproject.tar.xz
-rw-r--r-- 1 alice alice 196 Jun 20 10:00 myproject.tar.xz
7. Exclude Files
Archive everything except test files:
tar -czf myproject-no-tests.tar.gz --exclude="tests" myproject
tar -tzf myproject-no-tests.tar.gz
myproject/
myproject/src/
myproject/docs/
myproject/.gitignore
myproject/src/main.py
myproject/docs/guide.md
Exclude multiple patterns:
tar -czf myproject-filtered.tar.gz \
--exclude="tests" \
--exclude="*.log" \
--exclude=".git" \
myproject
8. Add Files to Existing Archive
Append a new file:
echo "README" > myproject/README.md
tar -rf myproject.tar myproject/README.md
tar -tf myproject.tar | grep README
myproject/README.md
9. Verbose Output
See all files as they're added:
tar -czvf myproject-verbose.tar.gz myproject
myproject/
myproject/src/
myproject/docs/
myproject/tests/
myproject/.gitignore
myproject/src/main.py
myproject/docs/guide.md
myproject/tests/test_main.py
10. Extract to a Specific Directory
Extract archive contents into /tmp/restore:
tar -xzf myproject.tar.gz -C /tmp/restore
ls /tmp/restore/myproject
myproject/
This is useful when you want to control where files land without polluting the current directory.
Common Use Cases
Backup a Directory with Date Stamp
tar -czf "/backups/home-$(date +%Y%m%d).tar.gz" /home/alice
Extract Specific Files from an Archive
tar -xf archive.tar.gz --wildcards '*.py'
Compare Compression Sizes
ls -lh myproject.tar* | awk '{print $5, $9}'
10K myproject.tar
312B myproject.tar.gz
268B myproject.tar.bz2
196B myproject.tar.xz
Incremental Backup with --listed-incremental
tar -czf backup-full.tar.gz --listed-incremental=backup.snap myproject
tar -czf backup-inc.tar.gz --listed-incremental=backup.snap myproject
Common Mistakes
1. Forgetting -f Before the Archive Name
tar -czf requires -f immediately before the archive name. tar -cz myproject.tar.gz myproject would use myproject.tar.gz as a compression option, not a filename.
2. Conflicting Compression Flags
tar -czjf archive.tar.gz myproject uses both -z (gzip) and -j (bzip2). Choose one: -z for gzip, -j for bzip2, -J for xz.
3. Using Absolute Paths (Security Risk)
tar -cf archive.tar /home/alice/myproject stores the full path. Extracting overwrites /home/alice/myproject. Use relative paths or strip with --strip-components=N.
4. Extracting Without Checking Contents First
Always run tar -tf archive.tar.gz first to see what's inside. This prevents accidental directory overwrites or extracting hundreds of files into the current folder.
Practice Questions
1. What does tar -xzf archive.tar.gz -C /target do?
It extracts the gzip-compressed archive into the /target directory.
2. How do you create a .tar.xz archive?
tar -cJf archive.tar.xz directory/
3. What's the difference between -z, -j, and -J?
-z uses gzip (fast, decent compression), -j uses bzip2 (slower, better compression), -J uses xz (slowest, best compression).
4. How do you exclude a directory when creating an archive?
tar -czf archive.tar.gz --exclude="dir_to_exclude" myproject/
5. Challenge: Write a tar command that creates a compressed backup of /etc but excludes /etc/ssl/private and saves with a date stamp.
sudo tar -czf "/backups/etc-$(date +%Y%m%d-%H%M%S).tar.gz" --exclude="/etc/ssl/private" /etc
Mini Project: Backup Rotation Script
#!/bin/bash
# backup_rotate.sh â Create daily backups with 7-day retention
# Usage: ./backup_rotate.sh /path/to/source
SOURCE="${1:?Usage: $0 /path/to/source}"
BACKUP_DIR="/backups"
TIMESTAMP=$(date +%Y%m%d)
FILENAME="backup-$TIMESTAMP.tar.gz"
SNAP_FILE="$BACKUP_DIR/backup.snap"
mkdir -p "$BACKUP_DIR"
if [ -f "$SNAP_FILE" ]; then
echo "Creating incremental backup..."
tar -czf "$BACKUP_DIR/$FILENAME" \
--listed-incremental="$SNAP_FILE" \
"$SOURCE" 2>/dev/null
else
echo "Creating full backup..."
tar -czf "$BACKUP_DIR/$FILENAME" \
--listed-incremental="$SNAP_FILE" \
"$SOURCE" 2>/dev/null
fi
echo "Created: $BACKUP_DIR/$FILENAME"
# Remove backups older than 7 days
echo "Cleaning backups older than 7 days..."
find "$BACKUP_DIR" -name "backup-*.tar.gz" -mtime +7 -delete
echo "Remaining backups:"
ls -lh "$BACKUP_DIR"/backup-*.tar.gz 2>/dev/null
Expected output:
Creating full backup...
Created: /backups/backup-20260620.tar.gz
Cleaning backups older than 7 days...
Remaining backups:
-rw-r--r-- 1 alice alice 312K Jun 14 10:00 /backups/backup-20260614.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 15 10:00 /backups/backup-20260615.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 16 10:00 /backups/backup-20260616.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 17 10:00 /backups/backup-20260617.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 18 10:00 /backups/backup-20260618.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 19 10:00 /backups/backup-20260619.tar.gz
-rw-r--r-- 1 alice alice 312K Jun 20 10:00 /backups/backup-20260620.tar.gz
FAQ
What's Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro