Command / Code
tar -czf - /important/data | ssh user@backup-server 'cat > /backups/remote-backup.tar.gz'
Description
1. Introduction
The tar command linux is an essential tool for archive management and backup operations that every Linux administrator must master. While file compression utilities are numerous, the tar command linux stands out as the most versatile and powerful solution for creating, extracting, and managing archives across different Linux distributions and Unix-like systems.
2. Tar Command Linux Essential Usage
Archive management becomes effortless when you understand the core functionality of this powerful utility. The tar command linux (Tape ARchive) was originally designed for magnetic tape storage but has evolved into the standard archiving tool for modern file systems.
Why mastering tar is crucial for Linux professionals:
- System backup creation with compression and preservation of permissions
- File distribution across networks and storage systems
- Directory synchronization for deployment and migration tasks
- Log rotation management for system maintenance workflows
- Package creation for software distribution and installation
The tar utility integrates seamlessly with compression tools covered in our Linux File Management Guide and complements backup strategies detailed in our System Administration Tutorials.
Related Linux Archive Tools:
- Advanced compression techniques: Linux Commands Reference
- Backup automation strategies: Linux Troubleshooting Guide
External Resources:
- GNU tar manual: gnu.org/software/tar/manual – comprehensive tar documentation
- Linux backup best practices: tldp.org/HOWTO/Backup-With-RSync-HOWTO – backup strategies guide
3. Essential Tar Command Examples
# Create archive with compression - Most common tar command linux usage
tar -czf backup.tar.gz /home/user/documents
# Extract archive with verbose output
tar -xzvf backup.tar.gz
# Create archive without compression for speed
tar -cf archive.tar /var/log
# List archive contents without extracting
tar -tzf backup.tar.gz
# Extract specific files from archive
tar -xzf backup.tar.gz path/to/specific/file.txt
# For more tar examples, visit our [Tar Command Reference](https://linuxtips.pro/commands/){:target="_blank"}
4. Advanced Tar Command Linux Techniques
# Professional tar workflows for system administrators
# 1. Create dated backup with progress indicator
tar -czf "backup-$(date +%Y-%m-%d).tar.gz" /home --checkpoint=1000 --checkpoint-action=echo
# 2. **Tar command linux** with exclude patterns for clean backups
tar -czf system-backup.tar.gz /etc /home --exclude='*.tmp' --exclude='*.cache'
# 3. Remote backup over SSH with tar streaming
tar -czf - /important/data | ssh user@backup-server 'cat > /backups/remote-backup.tar.gz'
# 4. Incremental backup with tar (covered in [System Monitoring](https://linuxtips.pro/guides/){:target="_blank"})
tar -czf incremental.tar.gz --listed-incremental=backup.snar /data
# Combine with our [Pro Community](https://linuxtips.pro/pro-community/){:target="_blank"} scripts for automated backup workflows
5. Detailed Command Explanation
The tar command linux syntax follows this pattern:
bashtar [OPERATION] [OPTIONS] [ARCHIVE-NAME] [FILES/DIRECTORIES]
Core Operations:
-c
(create) – Creates new archive from files/directories-x
(extract) – Extracts files from existing archive-t
(list) – Lists contents of archive without extraction-r
(append) – Adds files to existing archive-u
(update) – Adds files that are newer than archive versions
Essential Options:
-z
(gzip) – Compress/decompress archive using gzip algorithm-j
(bzip2) – Compress/decompress using bzip2 for better compression-J
(xz) – Compress/decompress using xz for maximum compression-v
(verbose) – Display processed files during operation-f
(file) – Specify archive filename (must be last option before filename)-p
(preserve) – Preserve file permissions, ownership, and timestamps-C
(directory) – Change to directory before processing files
Advanced Options:
--exclude=PATTERN
– Skip files matching specified pattern--checkpoint=N
– Display progress every N records processed--listed-incremental=FILE
– Create incremental backup using snapshot file--same-owner
– Preserve original file ownership (requires root)--sparse
– Handle sparse files efficiently--transform='EXPRESSION'
– Transform file names using sed expressions
6. Conclusion
Mastering the tar command linux transforms your file management capabilities from basic operations to professional-grade archive handling. This powerful utility provides the foundation for effective backup strategies, system deployment, and file distribution workflows essential for Linux system administration.
Whether you’re creating system backups, distributing software packages, or managing log rotations, the tar command linux should be your primary archiving solution. The combination of compression options, pattern matching, and network streaming capabilities makes it indispensable for modern Linux environments.
For advanced automation and scripting integration, explore our Linux Tips Collection and engage with our Pro Community for expert discussions on archive management optimization.
Advanced Learning Resources:
- Linux file system hierarchy: filesystem-hierarchy-standard.org – understanding directory structures for better archiving
- Compression algorithms comparison: tukaani.org/xz – detailed analysis of compression methods
7. Difficulty Level
βοΈ Intermediate
8. Reading Time
5 minutes
9. Rating
9/10
10. Command Category
File Management & Backup
11. Prerequisites
Basic Linux command line knowledge, understanding of file permissions and directory structures
12. Related Commands
bashgzip - Compress/decompress files using gzip algorithm
bzip2 - High-compression alternative to gzip
xz - Maximum compression utility for archives
zip - Cross-platform archive format with compression
unzip - Extract ZIP archives on Linux systems
rsync - Efficient file synchronization and backup
find - Locate files for selective archiving
du - Display disk usage before archiving
ls -la - List files with permissions for archive planning
chmod - Modify permissions before archiving
13. Performance Tips
Use tar with appropriate compression based on your needs: -z for speed, -j for balance, -J for maximum compression
14. Security Notes
Always verify archive integrity with checksums and be cautious when extracting archives from untrusted sources to prevent directory traversal attacks
15. Troubleshooting
For archive corruption issues, consult our Linux Troubleshooting Guide or contact our support team for expert assistance
Detailed Explanation
π Detailed Command Analysis
Part 1: tar -czf – /important/data
Tar Options:
-c = Create (creates a new archive)
-z = gZip (compresses using gzip)
-f = File (specifies the output filename)
– = STDOUT (sends output to standard output instead of a file)
/important/data = Source directory to archive
The – (dash) trick:
Normally you’d write tar -czf backup.tar.gz /important/data, but here the – tells tar to send the compressed data directly to the pipe instead of saving a local file.
Part 2: | (Pipe)
The pipe takes the output from the tar command (the compressed data stream) and passes it as input to the next command (ssh).
Part 3: ssh user@backup-server ‘cat > /backups/remote-backup.tar.gz’
SSH Connection:
ssh user@backup-server = connects to the remote server
‘cat > /backups/remote-backup.tar.gz’ = command executed on the remote server
The remote command cat > file:
cat reads data from the pipe
> redirects output to a file
/backups/remote-backup.tar.gz = final file on the remote server
π Step-by-Step Process
1. tar reads /important/data
2. tar compresses data with gzip
3. tar sends compressed stream to STDOUT (-)
4. The pipe (|) captures this stream
5. ssh connects to remote server
6. ssh sends the stream to the server
7. cat on server receives the stream
8. cat writes the stream to .tar.gz file
π‘ Advantages of This Method
β
Efficiency:
No local space: Doesn’t create temporary files on local computer
Direct streaming: Data goes directly from source to server
Real-time compression: Reduces network traffic
β Security:
SSH encryption: Data travels encrypted
Authentication: Uses SSH keys or passwords
No temporary files: Reduces data exposure risk
π Practical Examples
Complete system backup:
# Backup entire home directory
tar -czf – /home/myuser | ssh backup@192.168.1.100 ‘cat > /storage/backups/home-backup-$(date +%Y%m%d).tar.gz’
Backup with exclusions:
# Excludes cache and temporary files
tar -czf – –exclude=’*.tmp’ –exclude=’*/.cache’ /home/myuser | ssh backup@server ‘cat > /backups/clean-backup.tar.gz’
Backup with progress:
# Shows progress during backup
tar -czf – /important/data –checkpoint=1000 –checkpoint-action=echo | ssh user@server ‘cat > /backups/backup.tar.gz’
π§ Variants and Improvements
With xz compression (maximum compression):
tar -cJf – /data | ssh user@server ‘cat > /backups/max-compressed.tar.xz’
With integrity verification:
# Also creates MD5 checksum
tar -czf – /data | tee >(ssh user@server ‘cat > /backups/backup.tar.gz’) | md5sum > backup.md5
With completion notification:
tar -czf – /data | ssh user@server ‘cat > /backups/backup.tar.gz && echo “Backup completed at $(date)”‘
β οΈ Important Considerations
Error Handling:
# Version with error handling
tar -czf – /data 2>/dev/null | ssh user@server ‘cat > /backups/backup.tar.gz’ || echo “Backup failed!”
Bandwidth limiting:
# Limits bandwidth to 1MB/s
tar -czf – /data | ssh -o “ControlMaster=no” user@server ‘throttle -k 1024 cat > /backups/backup.tar.gz’
Resume capability:
# For very large backups, consider rsync instead
rsync -avz –partial –progress /data/ user@server:/backups/data/
π― When to Use This Method
β
Ideal for:
Automated backups with cron
Servers with limited local space
Secure and fast networks
One-time backups of specific directories
β Avoid if:
Unstable internet connection
Very large backups (>100GB)
Need to resume interrupted backups
Remote server with limited space
π Backup Restoration
To extract the backup from the remote server:
# Download and extract in one go
ssh user@backup-server ‘cat /backups/remote-backup.tar.gz’ | tar -xzf –
# Or download first, then extract
scp user@backup-server:/backups/remote-backup.tar.gz ./
tar -xzf remote-backup.tar.gz
π Advanced Use Cases
Database backup with compression:
# MySQL database backup to remote server
mysqldump –all-databases | gzip | ssh backup@server ‘cat > /backups/mysql-$(date +%Y%m%d).sql.gz’
Log rotation and remote archival:
# Archive old logs and send to remote storage
tar -czf – /var/log/*.log.1 | ssh logserver@archive ‘cat > /archives/logs-$(hostname)-$(date +%Y%m%d).tar.gz’
Multi-server backup coordination:
# Backup multiple servers in sequence
for server in web1 web2 web3; do
ssh $server ‘tar -czf – /var/www’ | ssh backup@storage “cat > /backups/${server}-$(date +%Y%m%d).tar.gz”
done
This command is a perfect example of how Unix tools can be combined to create powerful and efficient solutions! π
π§ Technical Benefits
Memory Efficiency:
Streaming operation: Uses minimal RAM regardless of archive size
No disk I/O overhead: Bypasses local filesystem completely
Pipeline parallelism: Compression and network transfer happen simultaneously
Network Optimization:
Compressed transmission: Reduces bandwidth usage by 60-80%
Single connection: Efficient use of SSH tunnel
No intermediate storage: Direct source-to-destination transfer
This technique showcases the power of Unix philosophy: combining simple tools to create sophisticated solutions! πͺ