tar Command Cheat Sheet
The tar (tape archive) command is a powerful archiving utility in Linux used to create, extract, list, and manipulate archive files. It can combine multiple files and directories into a single archive file and supports various compression formats.
Synopsis
tar [OPTION...] [FILE]...
Description
GNU tar saves many files together into a single tape or disk archive, and can restore individual files from the archive. It was originally developed for backing up data to tape drives, but is now commonly used for creating compressed file archives and distributing software packages.
Main Operation Modes
Only one of these options can be specified:
-c, --create
Create a new archive.
tar -cf archive.tar file1 file2 dir/
-x, --extract, --get
Extract files from an archive.
tar -xf archive.tar
-t, --list
List the contents of an archive.
tar -tf archive.tar
-r, --append
Append files to the end of an archive.
tar -rf archive.tar newfile.txt
-u, --update
Only append files newer than version in archive.
tar -uf archive.tar file.txt
-d, --diff, --compare
Find differences between archive and file system.
tar -df archive.tar
--delete
Delete from the archive (not on magnetic tapes).
tar --delete -f archive.tar file.txt
Compression Options
-z, --gzip, --gunzip
Filter the archive through gzip (create .tar.gz or .tgz files).
# Create
tar -czf archive.tar.gz directory/
# Extract
tar -xzf archive.tar.gz
-j, --bzip2
Filter through bzip2 (create .tar.bz2 or .tbz files).
# Create
tar -cjf archive.tar.bz2 directory/
# Extract
tar -xjf archive.tar.bz2
-J, --xz
Filter through xz (create .tar.xz files).
# Create
tar -cJf archive.tar.xz directory/
# Extract
tar -xJf archive.tar.xz
--lzip
Filter through lzip.
tar --lzip -cf archive.tar.lz files/
--lzma
Filter through lzma.
tar --lzma -cf archive.tar.lzma files/
--lzop
Filter through lzop.
tar --lzop -cf archive.tar.lzo files/
--zstd
Filter through zstd (Zstandard compression).
tar --zstd -cf archive.tar.zst directory/
-a, --auto-compress
Use archive suffix to determine compression program automatically.
tar -caf archive.tar.gz files/ # Auto-detects gzip
tar -caf archive.tar.xz files/ # Auto-detects xz
Common Options
-f, --file=ARCHIVE
Specify the archive file name (required for most operations).
tar -cf myarchive.tar files/
-v, --verbose
Verbosely list files processed (show file names).
tar -czvf archive.tar.gz dir/
Output example:
dir/
dir/file1.txt
dir/file2.txt
-C, --directory=DIR
Change to directory DIR before performing operations.
# Extract to specific directory
tar -xzf archive.tar.gz -C /tmp/
# Create archive from specific directory
tar -czf backup.tar.gz -C /home/user documents/
-p, --preserve-permissions
Preserve permissions when extracting (default for superuser).
tar -xpf archive.tar
-P, --absolute-names
Don't strip leading '/' from file names.
tar -cPf backup.tar /etc/config
--exclude=PATTERN
Exclude files matching pattern.
# Exclude .git directories
tar -czf backup.tar.gz --exclude='.git' project/
# Exclude multiple patterns
tar -czf backup.tar.gz --exclude='*.log' --exclude='*.tmp' project/
--exclude-from=FILE
Read exclude patterns from file.
# Create .tarignore file
echo "*.log" > .tarignore
echo "node_modules/" >> .tarignore
tar -czf backup.tar.gz --exclude-from=.tarignore project/
-h, --dereference
Follow symbolic links; archive the files they point to.
tar -czfh archive.tar.gz directory/
-X, --exclude-from=FILE
Exclude files listed in FILE.
tar -czf archive.tar.gz -X exclude-list.txt /data
Advanced Options
--strip-components=NUMBER
Strip NUMBER leading components from file names on extraction.
# Archive contains: project/src/main.c
# Extract as: main.c
tar -xzf archive.tar.gz --strip-components=2
--sparse, -S
Handle sparse files efficiently.
tar -cSzf archive.tar.gz large-database.db
-T, --files-from=FILE
Get names to extract or create from FILE.
# Create file with list
ls /etc/*.conf > files.txt
tar -czf configs.tar.gz -T files.txt
--numeric-owner
Use numeric user/group IDs instead of names.
tar -czf --numeric-owner backup.tar.gz /home
-W, --verify
Verify the archive after writing.
tar -czWf archive.tar.gz directory/
--totals
Print total bytes after processing.
tar -czf archive.tar.gz --totals directory/
--checkpoint[=N]
Display progress messages every Nth record (default 10).
tar -czf archive.tar.gz --checkpoint=1000 large-dir/
-k, --keep-old-files
Don't replace existing files when extracting.
tar -xzkf archive.tar.gz
--overwrite
Overwrite existing files when extracting.
tar -xzf archive.tar.gz --overwrite
--one-file-system
Stay in local file system when creating archive.
tar -czf backup.tar.gz --one-file-system /
Practical Examples
Create Basic Archive
tar -cf archive.tar file1.txt file2.txt directory/
Create Compressed Archive (Recommended)
# Using gzip (good compression, fast)
tar -czf project.tar.gz project/
# Using bzip2 (better compression, slower)
tar -cjf project.tar.bz2 project/
# Using xz (best compression, slowest)
tar -cJf project.tar.xz project/
Extract Archive
# Extract to current directory
tar -xzf archive.tar.gz
# Extract to specific directory
tar -xzf archive.tar.gz -C /target/path/
# Extract specific files only
tar -xzf archive.tar.gz file.txt dir/subdir/
List Archive Contents
# Simple list
tar -tzf archive.tar.gz
# Detailed list (with permissions, size, date)
tar -tzvf archive.tar.gz
View Without Extracting
# View specific file
tar -xzf archive.tar.gz path/to/file.txt -O | less
# Search in archive
tar -xzf archive.tar.gz -O | grep "search pattern"
Backup with Timestamp
tar -czf backup-$(date +%Y%m%d-%H%M%S).tar.gz /important/data/
Backup and Exclude Files
# Exclude version control
tar -czf backup.tar.gz --exclude-vcs project/
# Exclude cache and logs
tar -czf backup.tar.gz \
--exclude='*.log' \
--exclude='cache/*' \
--exclude='node_modules' \
website/
Split Large Archives
# Create archive and split into 100MB chunks
tar -czf - large-dir/ | split -b 100M - archive.tar.gz.part
# Recombine and extract
cat archive.tar.gz.part* | tar -xz
Archive with Preserved Permissions
# Full backup with all attributes
sudo tar -czpf backup.tar.gz /etc/
# Restore with original permissions
sudo tar -xzpf backup.tar.gz -C /
Incremental Backup
# Full backup (level 0)
tar -czg snapshot.file -f full-backup.tar.gz /data/
# Incremental backup (only changed files)
tar -czg snapshot.file -f incremental-backup.tar.gz /data/
Append to Existing Archive
# Append files (uncompressed archives only)
tar -rf archive.tar newfile.txt
# For compressed, must extract, add, and recompress
tar -xzf archive.tar.gz
tar -czf archive.tar.gz existing/ newfile.txt
Transfer Over Network
# Send to remote server
tar -czf - /data | ssh user@server 'cat > backup.tar.gz'
# Extract from remote server
ssh user@server 'cat backup.tar.gz' | tar -xz
# Direct extraction on remote
tar -czf - /data | ssh user@server 'tar -xzC /destination'
Compare Archive with Filesystem
tar -dzf backup.tar.gz /data/
Create Archive with Progress Bar
# Using pv (pipe viewer)
tar -cf - directory/ | pv | gzip > archive.tar.gz
# With checkpoint
tar -czf archive.tar.gz --checkpoint=.1000 large-dir/
Exit Status
| Code | Meaning |
|---|---|
| 0 | Successful completion |
| 1 | Some files differ (when using --compare) |
| 2 | Fatal error |
Common Use Cases
Website Backup
tar -czf website-$(date +%F).tar.gz \
--exclude='*.log' \
--exclude='cache/*' \
/var/www/html/
Database Backup
mysqldump database_name | gzip | \
tar -czf db-backup-$(date +%F).tar.gz -T -
Home Directory Backup
tar -czf ~/backup-home-$(date +%F).tar.gz \
--exclude='.cache' \
--exclude='Downloads' \
--exclude='.local/share/Trash' \
~/
Configuration Backup
sudo tar -czpf /backup/etc-$(date +%F).tar.gz /etc/
Tips and Best Practices
- Always use compression for storage efficiency (
-z,-j, or-J) - Use verbose mode (
-v) to see what's being archived - Test archives after creation:
tar -tzf archive.tar.gz - Exclude unnecessary files to reduce archive size
- Use absolute paths carefully - avoid
-Punless necessary - Verify archives with
-Wor--verifyfor critical backups - Choose compression based on needs:
- gzip (-z): Fast, good compression, widely compatible
- bzip2 (-j): Better compression, slower
- xz (-J): Best compression, slowest, smaller files
- zstd (--zstd): Fast compression/decompression, good ratio