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
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
| 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 |
Example 1Create a Basic Archive
Create a simple tar archive without compression:
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
-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:
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
.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:
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
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:
Explanation:
-t: List contents (table of contents)-z: Handle gzip compression-f: Specify archive file- No files are extracted, only listed
- Piping to
headshows first 10 entries - Use
-vfor detailed listing with permissions
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:
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
-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:
Explanation:
- Specify paths after archive name to extract selectively
- First command extracts entire docs directory
- Second command extracts single specific file
--wildcardsenables pattern matching- Paths must match exactly as stored in archive
- Use
tar -tffirst to see exact paths
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:
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
--exclude-from=exclude.txt for complex projects with many exclusions.
Example 8Incremental Backups
Create incremental backups that only include 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
Example 9Remote Archives Over SSH
Create and extract archives on remote systems:
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
-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:
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
--same-owner requires root privileges. Without it, extracted files will be owned by the extracting user, potentially breaking applications that depend on specific ownership.
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/
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/
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.
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.
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.
Monitor progress of large operations:
Use pv (pipe viewer) for throughput, or --checkpoint for progress dots every N records.
Test archives before relying on them:
For critical backups, create checksums: tar -czf backup.tar.gz dir/ && sha256sum backup.tar.gz > backup.tar.gz.sha256
Database files and disk images with large zero regions:
The -S or --sparse flag handles sparse files efficiently, storing zero-filled regions specially.
Standard exclusions for source code archives:
--exclude-vcs automatically excludes .git, .svn, .hg, CVS, etc. --exclude-backups skips *~, .bak files.
Split archives across multiple files for media with size limits:
Useful for FAT32 filesystems (4GB limit) or optical media. The dot notation creates backup.tar.gz.aa, backup.tar.gz.ab, etc.
Use parallel compression tools for large archives:
Can reduce compression time by 50-75% on multi-core systems.
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.
| 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' |
| 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) |