tar - Tape Archive

The Universal Unix Archiving and Compression Tool

About tar

The tar (tape archive) command is one of the most essential Unix/Linux utilities for creating archives, backing up files, and distributing software. Despite its name referencing magnetic tape, tar is used universally for file archiving with or without compression.

Common Use Cases:

  • Creating compressed backups of directories and files
  • Distributing source code and software packages
  • Preserving file permissions, ownership, and timestamps
  • Transferring directory structures between systems
  • Creating incremental and differential backups
  • Archiving log files and system configurations
  • Remote backups over SSH connections
Remembering tar Options: Think of the classic trio:
c = create, x = extract, t = list
Then add: v = verbose (see what's happening), f = file (the archive name)
For compression: z = gzip, j = bzip2, J = xz
Common Options Reference
Option Description
-c Create a new archive
-x Extract files from an archive
-t List contents of an archive
-v Verbose mode (show files being processed)
-f Specify archive filename (must be followed by filename)
-z Compress/decompress using gzip (.tar.gz or .tgz)
-j Compress/decompress using bzip2 (.tar.bz2)
-J Compress/decompress using xz (.tar.xz)
-C Change to directory before operation
-p Preserve file permissions (default for root)
--exclude Exclude files/directories matching pattern
-r Append files to an existing archive
-u Update: only append files newer than archive copy
--listed-incremental Create incremental backup using snapshot file
--wildcards Enable wildcard pattern matching for extraction
Detailed Examples

Example 1Create a Basic Archive

Create a simple tar archive without compression:

$ mkdir -p myproject/{src,docs,config} $ touch myproject/src/main.c myproject/docs/README.md myproject/config/settings.conf $ tar -cvf myproject.tar myproject/
myproject/ myproject/src/ myproject/src/main.c myproject/docs/ myproject/docs/README.md myproject/config/ myproject/config/settings.conf

Explanation:

  • -c: Create new archive
  • -v: Verbose mode shows each file being archived
  • -f myproject.tar: Output filename
  • Directory structure is preserved in the archive
  • No compression applied (.tar extension indicates this)
  • Original files remain unchanged
Note: The -f option must be the last option before the filename. Modern tar allows -cvf or -c -v -f, but -f must immediately precede the archive name.

Example 2Create Compressed Archives (gzip, bzip2, xz)

Create archives with different compression algorithms:

$ tar -czf myproject.tar.gz myproject/ # gzip compression $ tar -cjf myproject.tar.bz2 myproject/ # bzip2 compression $ tar -cJf myproject.tar.xz myproject/ # xz compression $ ls -lh myproject.tar*
-rw-r--r-- 1 user user 10K Dec 16 10:30 myproject.tar -rw-r--r-- 1 user user 2.1K Dec 16 10:31 myproject.tar.bz2 -rw-r--r-- 1 user user 2.8K Dec 16 10:30 myproject.tar.gz -rw-r--r-- 1 user user 2.0K Dec 16 10:31 myproject.tar.xz

Explanation:

  • -z: Use gzip compression (fast, good compression)
  • -j: Use bzip2 compression (slower, better compression)
  • -J: Use xz compression (slowest, best compression)
  • .tar.gz (or .tgz) is most common and widely compatible
  • xz typically provides best compression ratio
  • gzip is fastest for both compression and decompression
Tip: Use .tgz as shorthand for .tar.gz and .tbz for .tar.bz2. Modern tar can auto-detect compression on extraction, but explicit extensions help users identify the format.

Example 3Extract Archives

Extract files from various archive formats:

$ tar -xvf myproject.tar # Extract uncompressed $ tar -xzf myproject.tar.gz # Extract gzip $ tar -xjf myproject.tar.bz2 # Extract bzip2 $ tar -xJf myproject.tar.xz # Extract xz # Modern tar can auto-detect compression: $ tar -xvf myproject.tar.gz # Works without -z
myproject/ myproject/src/ myproject/src/main.c myproject/docs/ myproject/docs/README.md myproject/config/ myproject/config/settings.conf

Explanation:

  • -x: Extract files from archive
  • -v: Show files as they're extracted
  • Compression flags match those used for creation
  • GNU tar can auto-detect compression format
  • Files extracted to current directory
  • Directory structure is recreated exactly
Warning: Always inspect archive contents with tar -tf before extracting to avoid "tar bombs" (archives that extract many files directly to current directory instead of a subdirectory).

Example 4List Archive Contents Without Extracting

View what's inside an archive before extracting:

$ tar -tzf myproject.tar.gz | head -10
myproject/ myproject/src/ myproject/src/main.c myproject/docs/ myproject/docs/README.md myproject/config/ myproject/config/settings.conf

Explanation:

  • -t: List contents (table of contents)
  • -z: Handle gzip compression
  • -f: Specify archive file
  • No files are extracted, only listed
  • Piping to head shows first 10 entries
  • Use -v for detailed listing with permissions
Tip: Use tar -tvzf archive.tar.gz for detailed listing showing permissions, owners, sizes, and dates - similar to ls -l output.

Example 5Extract to Specific Directory

Extract archive contents to a different location:

$ mkdir /tmp/restore $ tar -xzf myproject.tar.gz -C /tmp/restore $ ls /tmp/restore/
myproject

Explanation:

  • -C /tmp/restore: Change to directory before extracting
  • Target directory must exist before extraction
  • Archive extracts into the specified directory
  • Current working directory remains unchanged
  • Useful for organizing extractions or restoring to alternate locations
  • Can be combined with any extraction options
Note: The -C option must come before the archive filename in the command. Think of it as "change directory, then operate on this file."

Example 6Selective Extraction Using Patterns

Extract only specific files or directories from an archive:

$ tar -xzf myproject.tar.gz myproject/docs/ $ tar -xzf myproject.tar.gz myproject/src/main.c $ tar -xzf myproject.tar.gz --wildcards '*.conf'
myproject/docs/ myproject/docs/README.md myproject/src/main.c myproject/config/settings.conf

Explanation:

  • Specify paths after archive name to extract selectively
  • First command extracts entire docs directory
  • Second command extracts single specific file
  • --wildcards enables pattern matching
  • Paths must match exactly as stored in archive
  • Use tar -tf first to see exact paths
Tip: To extract all .c files: tar -xzf archive.tar.gz --wildcards '*.c'. For case-insensitive matching, add --ignore-case.

Example 7Excluding Files and Directories

Create archives while excluding specific patterns:

$ tar -czf backup.tar.gz \ --exclude='*.log' \ --exclude='*.tmp' \ --exclude='.git' \ --exclude='node_modules' \ myproject/
myproject/ myproject/src/ myproject/src/main.c myproject/docs/ myproject/docs/README.md myproject/config/ myproject/config/settings.conf

Explanation:

  • --exclude='*.log': Exclude all .log files
  • --exclude='.git': Exclude .git directories
  • Multiple --exclude options can be combined
  • Patterns use shell globbing syntax
  • Essential for avoiding large cache/build directories
  • Significantly reduces archive size for code projects
Tip: Create an exclude file with patterns (one per line) and use --exclude-from=exclude.txt for complex projects with many exclusions.

Example 8Incremental Backups

Create incremental backups that only include changed files:

$ tar -czf full-backup.tar.gz \ --listed-incremental=backup.snar \ myproject/ # Make some changes $ echo "new content" > myproject/src/update.c # Create incremental backup $ tar -czf incremental-backup.tar.gz \ --listed-incremental=backup.snar \ myproject/
Full backup: 2.8K Incremental backup: 0.5K (only changed files)

Explanation:

  • --listed-incremental: Enable incremental backup mode
  • backup.snar: Snapshot file tracking file states
  • First run creates full backup and snapshot
  • Subsequent runs only backup changed files
  • Snapshot file must be preserved for restore
  • Dramatically reduces backup size and time
Note: To restore incremental backups, extract the full backup first, then extract each incremental in order. The snapshot file (.snar) is critical for the incremental process.

Example 9Remote Archives Over SSH

Create and extract archives on remote systems:

$ tar -czf - myproject/ | ssh user@remote 'cat > /backup/myproject.tar.gz' # Or extract directly on remote system: $ tar -czf - myproject/ | ssh user@remote 'tar -xzf - -C /destination/' # Pull archive from remote system: $ ssh user@remote 'tar -czf - /data/myproject' > local-backup.tar.gz
Streaming archive over SSH connection... Archive created/extracted on remote system

Explanation:

  • - (dash): Use stdin/stdout instead of file
  • First command: creates archive locally, saves remotely
  • Second command: creates locally, extracts remotely (one step)
  • Third command: creates remotely, saves locally
  • No temporary files needed - pure streaming
  • Efficient for network transfers with compression
Tip: Add -v flag on the receiving end to monitor progress: tar -xzvf -. For better performance over slow networks, consider adjusting compression level or using faster algorithms.

Example 10Preserving Permissions and Ownership

Create and restore archives maintaining exact file attributes:

$ sudo tar -czpf system-backup.tar.gz \ --numeric-owner \ --preserve-permissions \ --same-owner \ /etc/apache2/ /etc/nginx/ # Restore with preserved attributes (requires root): $ sudo tar -xzpf system-backup.tar.gz \ --preserve-permissions \ --same-owner \ -C /
etc/apache2/ etc/apache2/apache2.conf (root:root, 0644) etc/nginx/ etc/nginx/nginx.conf (root:root, 0644)

Explanation:

  • -p: Preserve permissions (mode, ownership, times)
  • --numeric-owner: Store UIDs/GIDs as numbers
  • --preserve-permissions: Explicit permission preservation
  • --same-owner: Restore original ownership (needs root)
  • Critical for system configuration backups
  • Ensures restored files have correct security settings
Warning: Restoring with --same-owner requires root privileges. Without it, extracted files will be owned by the extracting user, potentially breaking applications that depend on specific ownership.
Related Commands and Tools
Common Pitfalls and Solutions
Pitfall 1: The "Tar Bomb" - Files Extracting Everywhere

Archives without a top-level directory scatter files across current directory.

Solution: Always check with tar -tf archive.tar.gz | head before extracting. If no common directory prefix, create one: mkdir extract && tar -xzf archive.tar.gz -C extract/

Pitfall 2: Leading Slashes in Archive Paths

Archives with absolute paths (starting with /) can overwrite system files.

Solution: Modern tar strips leading slashes by default. To preserve them (dangerous!), use -P. When creating archives, use relative paths: tar -czf backup.tar.gz -C / etc/ instead of tar -czf backup.tar.gz /etc/

Pitfall 3: Wrong Order of Options with -f

Command tar -cfz archive.tar.gz dir/ fails or creates wrong file.

Solution: The -f option must come last before the filename: tar -czf archive.tar.gz dir/. Whatever follows -f is treated as the archive name.

Pitfall 4: Disk Space Exhaustion During Extraction

Large archives can fill up the filesystem during extraction.

Solution: Check available space first: df -h . and compare with archive size. Use tar -tvf to estimate uncompressed size. Extract to filesystem with sufficient space using -C.

Pitfall 5: Lost Permissions When Not Using Root

System backups extracted as regular user lose ownership information.

Solution: For system backups, always create with sudo tar -czpf and restore with sudo tar -xzpf. Use --numeric-owner when creating for portability across systems.

Pro Tips and Best Practices
Tip 1: Progress Indication for Large Archives

Monitor progress of large operations:

tar -czf - large-dir/ | pv > backup.tar.gz tar -xzf backup.tar.gz --checkpoint=1000 --checkpoint-action=dot

Use pv (pipe viewer) for throughput, or --checkpoint for progress dots every N records.

Tip 2: Verify Archive Integrity

Test archives before relying on them:

tar -tzf backup.tar.gz > /dev/null && echo "Archive OK" || echo "Archive CORRUPTED"

For critical backups, create checksums: tar -czf backup.tar.gz dir/ && sha256sum backup.tar.gz > backup.tar.gz.sha256

Tip 3: Handling Sparse Files Efficiently

Database files and disk images with large zero regions:

tar -czSf sparse-backup.tar.gz --sparse database.img

The -S or --sparse flag handles sparse files efficiently, storing zero-filled regions specially.

Tip 4: Exclude Version Control

Standard exclusions for source code archives:

tar -czf project.tar.gz \ --exclude-vcs \ --exclude-backups \ myproject/

--exclude-vcs automatically excludes .git, .svn, .hg, CVS, etc. --exclude-backups skips *~, .bak files.

Tip 5: Multi-Volume Archives for Large Datasets

Split archives across multiple files for media with size limits:

tar -czf - large-dataset/ | split -b 4G - backup.tar.gz. cat backup.tar.gz.* | tar -xzf -

Useful for FAT32 filesystems (4GB limit) or optical media. The dot notation creates backup.tar.gz.aa, backup.tar.gz.ab, etc.

Tip 6: Faster Compression with Multiple Cores

Use parallel compression tools for large archives:

tar -cf - dir/ | pigz -9 -p 4 > backup.tar.gz # parallel gzip tar -cf - dir/ | pbzip2 -9 -p4 > backup.tar.bz2 # parallel bzip2 tar -cf - dir/ | pxz -9 -T 4 > backup.tar.xz # parallel xz

Can reduce compression time by 50-75% on multi-core systems.

Historical Note: The tar command was developed in the early 1970s for backing up Unix systems to magnetic tape drives. The format has evolved significantly, but maintains backward compatibility. GNU tar (most common on Linux) includes many extensions beyond the POSIX standard, while BSD tar (macOS) differs in some features. Always test archives if moving between different Unix variants.
Quick Reference Cheat Sheet
Task Command
Create compressed archive tar -czf archive.tar.gz directory/
Extract compressed archive tar -xzf archive.tar.gz
List archive contents tar -tzf archive.tar.gz
Extract to specific directory tar -xzf archive.tar.gz -C /path/
Create with exclusions tar -czf backup.tar.gz --exclude='*.log' dir/
Extract specific files tar -xzf archive.tar.gz path/to/file
Best compression (xz) tar -cJf archive.tar.xz directory/
Preserve all permissions sudo tar -czpf backup.tar.gz --numeric-owner dir/
Incremental backup tar -czf backup.tar.gz --listed-incremental=snap.snar dir/
Remote backup via SSH tar -czf - dir/ | ssh user@host 'cat > backup.tar.gz'
Compression Format Comparison
Format Extension Speed Ratio Best Use Case
gzip (-z) .tar.gz, .tgz Fast Good General purpose, daily backups
bzip2 (-j) .tar.bz2, .tbz Medium Better Archival storage, better compression
xz (-J) .tar.xz Slow Best Software distribution, maximum compression
None .tar Fastest None Streaming, bundling (compress separately)