tar Command
The tar command (tape archive) creates and extracts archive files in Linux and Unix systems. It bundles multiple files and directories into a single archive file, making it essential for backups, file distribution, and system administration.
Syntax
Description
The tar command originally stood for "tape archive" but is now used for creating archive files on any storage medium. It preserves file permissions, ownership, timestamps, and directory structure while combining multiple files into a single archive.
Key features:
- Create archives from files and directories
- Extract files from existing archives
- List archive contents without extracting
- Support for various compression formats
- Preserve file metadata and permissions
📝 Memory Helper
Common tar options mnemonic:
- c = Create archive
- x = eXtract archive
- t = lisT contents
- f = File (specify archive name)
- v = Verbose output
- z = gZip compression
Compression Options
- -z (gzip): Fast compression, .tar.gz or .tgz extension
- -j (bzip2): Better compression, .tar.bz2 extension
- -J (xz): Best compression, .tar.xz extension
- No compression: Fastest, .tar extension
Common Archive Extensions
- .tar - Uncompressed tar archive
- .tar.gz / .tgz - Gzip compressed tar archive
- .tar.bz2 / .tbz2 - Bzip2 compressed tar archive
- .tar.xz - XZ compressed tar archive
Examples
Create archives
tar -czf backup.tar.gz documents/ # Create gzip compressed archive
tar -cjf backup.tar.bz2 documents/ # Create bzip2 compressed archive
tar -cJf backup.tar.xz documents/ # Create xz compressed archive
Create archives with different compression methods
Extract archives
tar -xzf backup.tar.gz # Extract gzip archive
tar -xjf backup.tar.bz2 # Extract bzip2 archive
tar -xf backup.tar.xz # Auto-detect compression
Extract archives (tar auto-detects compression in newer versions)
List archive contents
tar -tvf backup.tar # List with detailed info
tar -tzf backup.tar.gz | head -10 # List first 10 files in compressed archive
View archive contents without extracting
Verbose operations
tar -xvf backup.tar # Extract with verbose output
# Output shows each file being processed
See detailed progress during archive operations
Extract to specific directory
tar -xzf backup.tar.gz -C restore/ # Extract compressed archive to restore/
Extract files to a different directory using -C option
Exclude files and patterns
tar -czf backup.tar.gz --exclude-from=exclude.txt project/
tar -czf backup.tar.gz --exclude="node_modules" --exclude="*.log" project/
Create archives while excluding specific files or patterns
💡 Tips and Best Practices
- Always use -f: Specify filename to avoid confusion
- Test extractions: Use -t to list contents before extracting
- Use compression: Save space with -z, -j, or -J options
- Preserve paths: tar preserves directory structure by default
- Check disk space: Ensure enough space before extracting large archives
- Use absolute paths carefully: Can overwrite system files
Common Use Cases
- System backups:
tar -czf backup-$(date +%Y%m%d).tar.gz /home/user/ - Software distribution:
tar -czf myapp-1.0.tar.gz myapp/ - Log archival:
tar -czf logs-$(date +%Y%m).tar.gz /var/log/ - Project backup:
tar -czf project.tar.gz --exclude=".git" project/ - Remote backup:
tar -czf - /home/user/ | ssh server "cat > backup.tar.gz"