Skip to content
tar Command in Linux — Archive Files with Compression Examples

tar Command in Linux — Archive Files with Compression Examples

DodaTech Updated Jun 20, 2026 6 min read

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<br/>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]
OptionDescription
-cCreate a new archive
-xExtract files from archive
-tList archive contents
-fSpecify archive filename
-vVerbose (list files being processed)
-zFilter through gzip (.tar.gz)
-jFilter through bzip2 (.tar.bz2)
-JFilter through xz (.tar.xz)
--excludeExclude files matching pattern
-C DIRChange to directory before extracting
-rAppend files to existing archive
-uUpdate — 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 the difference between .tar.gz and .tgz?
Nothing — .tgz is just a shorter extension for .tar.gz. They’re identical. Extract with tar -xzf file.tgz.
How do I extract a single file from a large archive?
Use tar -xf archive.tar.gz path/to/file — provide the exact path shown by tar -tf.
Can tar compress on the fly without creating a file?
Yes: tar -czf - myproject/ | ssh user@host "tar -xzf - -C /dest" pipes the archive over SSH.
What does --strip-components=1 do?
It removes the top-level directory from extracted paths. If an archive contains myproject/src/main.py, --strip-components=1 extracts to src/main.py.

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