In Linux system administration and daily operations, file compression and decompression are among the most fundamental and frequently performed tasks. Choosing the right compression tools and parameters can not only save storage space and improve transfer efficiency but also play a crucial role in scenarios such as backup archiving and cross-platform collaboration. This article systematically introduces the mainstream compression tools in Linux, their uses, syntax, practical commands, and engineering practices, helping you quickly master this essential skill.
🎯 Comparison of Compression Formats and Their Uses
Understanding the characteristics of different compression formats is the basis for making the right choice. Below is a comparison of various mainstream formats:
| Format | Compression Algorithm | Compression Ratio | Speed | Main Features | Applicable Scenarios |
|---|---|---|---|---|---|
<span><span>.tar</span></span> |
None | None | Very Fast | Packs files without compression, retains permissions | File archiving, directory packaging |
<span><span>.tar.gz</span></span> |
gzip (DEFLATE) | Medium to High | Fast | Balances compression ratio and speed | Software distribution, regular backups ⭐ |
<span><span>.tar.bz2</span></span> |
bzip2 | High | Medium | Compression ratio 10%-15% higher than gzip | Space-saving backups |
<span><span>.tar.xz</span></span> |
LZMA2 | Very High | Slow | Highest compression ratio, CPU intensive | Distribution software packages |
<span><span>.zip</span></span> |
DEFLATE | Medium | Relatively Fast | Best cross-platform compatibility, supports encryption | Windows/Linux exchange ⭐ |
<span><span>.gz</span></span> |
gzip | Medium | Fast | Fast compression of single files | Log file compression |
<span><span>.bz2</span></span> |
bzip2 | High | Medium | High compression ratio for single files | Large file archiving |
<span><span>.xz</span></span> |
LZMA2 | Very High | Slow | Extreme compression for single files | Long-term storage |
<span><span>.7z</span></span> |
LZMA/LZMA2 | Very High | Slow | Compression ratio close to the limit | Requires maximum compression |
<span><span>.rar</span></span> |
RAR | High | Medium | Commercial format, commonly used in Windows | Cross-platform compatibility |
Selection Recommendations:
- 🔥 Daily Backups:
<span>.tar.gz</span><span> (best balance)</span> - 🌍 Cross-Platform Sharing:
<span>.zip</span><span> (best compatibility)</span> - 💾 Extreme Compression:
<span>.tar.xz</span>or<span>.7z</span> - ⚡ Fast Processing:
<span>.gz</span><span> (speed priority)</span>
📜 Command Syntax and Detailed Explanation
1. tar – The Linux Archiving Tool ⭐
Uses:
- Packs multiple files/directories into a single archive file
- Combines with gzip/bzip2/xz for compression
- Retains file permissions, owners, timestamps, and other metadata
- Incremental and differential backups
Basic Syntax:
# Create a compressed package
tar [options] -f target_file.tar[.gz|.bz2|.xz] source_file/directory
# Extract a compressed package
tar [options] -f source_file.tar[.gz|.bz2|.xz] [-C target_directory]
# View contents of a compressed package
tar -tf file.tar.gz
Core Options:
<span>-c</span>: Create new archive (create)<span>-x</span>: Extract archive (extract)<span>-t</span>: List contents (list)<span>-f</span>: Specify filename (file) ⚠️ Required<span>-v</span>: Show detailed process (verbose)<span>-z</span>: gzip compression/decompression (.tar.gz)<span>-j</span>: bzip2 compression/decompression (.tar.bz2)<span>-J</span>: xz compression/decompression (.tar.xz)<span>-C</span>: Specify extraction directory<span>--exclude</span>: Exclude files<span>-p</span>: Retain permissions (enabled by default)
Common Command Examples:
# ========== Create Compressed Package ==========
# Pack directory (no compression)
tar -cvf project.tar /path/to/project
# Pack and compress with gzip (recommended, fast)
tar -czvf project.tar.gz /path/to/project
# Pack and compress with bzip2 (higher compression ratio)
tar -cjvf project.tar.bz2 /path/to/project
# Pack and compress with xz (highest compression ratio, slow)
tar -cJvf project.tar.xz /path/to/project
# Pack excluding specific files
tar -czvf backup.tar.gz --exclude='*.log' --exclude='node_modules' /project
# Pack multiple directories
tar -czvf multi.tar.gz /etc /home/user1 /var/log
# ========== Extract Compressed Package ==========
# Extract tar.gz file
tar -xzvf project.tar.gz
# Extract to specified directory
tar -xzvf project.tar.gz -C /target/directory
# Extract tar.bz2 file
tar -xjvf project.tar.bz2
# Extract tar.xz file
tar -xJvf project.tar.xz
# Extract single file
tar -xzvf backup.tar.gz path/to/specific/file.txt
# ========== View Compressed Package ==========
# View contents of compressed package (without extraction)
tar -tzvf project.tar.gz
# View and display detailed permission information
tar -tvf project.tar.gz
# View tar.bz2 contents
tar -tjvf project.tar.bz2
Engineering Practice Tips:
# Show progress during compression (requires pv tool)
tar -czf - /large/directory | pv -s $(du -sb /large/directory | awk '{print $1}') > backup.tar.gz
# Split compress large files (2GB each)
tar -czf - /large/directory | split -b 2G - backup.tar.gz.part
# Merge and extract split files
cat backup.tar.gz.part* | tar -xzf -
# Test integrity of compressed package
tar -tzf backup.tar.gz > /dev/null && echo "Compressed package is intact" || echo "Compressed package is corrupted"
# Incremental backup (only back up changed files)
tar -czvf incremental.tar.gz --listed-incremental=snapshot.file /data
2. gzip/gunzip – Fast Single File Compression
Uses:
- Fast compression of single files (default compression ratio is good)
- Automatic compression archiving of log files
- Stream compression (pipeline operations)
Basic Syntax:
# Compress file
gzip [options] filename
# Decompress file
gunzip [options] filename.gz
# or
gzip -d filename.gz
Common Options:
<span>-d</span>: Decompress (decompress)<span>-k</span>: Keep original file (keep)<span>-c</span>: Output to standard output<span>-1</span>to<span>-9</span>: Compression level (1 fastest, 9 most compressed, default 6)<span>-r</span>: Recursively compress all files in the directory<span>-v</span>: Show compression ratio
Common Command Examples:
# ========== Compress File ==========
# Basic compression (original file will be replaced with .gz)
gzip access.log
# Keep original file compressed
gzip -k access.log
# or
gzip -c access.log > access.log.gz
# Highest compression level
gzip -9 large_file.db
# Fastest compression speed
gzip -1 temp_file.txt
# Recursively compress all files in the directory
gzip -r /var/log/old_logs/
# ========== Decompress File ==========
# Basic decompression
gunzip access.log.gz
# Keep .gz file during decompression
gunzip -k access.log.gz
# Decompress to standard output (do not delete original file)
gzip -dc access.log.gz > access.log
# ========== View Compressed File ==========
# Directly view compressed file contents (without decompression)
zcat access.log.gz
# Paginated view
zcat access.log.gz | less
# Search compressed file contents
zgrep "ERROR" access.log.gz
# View compression information
gzip -l access.log.gz
3. bzip2/bunzip2 – High Compression Rate Tool
Uses:
- Higher compression rate than gzip (about 10%-15% higher)
- Suitable for scenarios requiring high compression rates
- CPU intensive, slower compression and decompression speed
Basic Syntax:
# Compress file
bzip2 [options] filename
# Decompress file
bunzip2 [options] filename.bz2
# or
bzip2 -d filename.bz2
Common Command Examples:
# ========== Compress File ==========
# Basic compression
bzip2 data.txt
# Keep original file
bzip2 -k large_file.sql
# Highest compression level
bzip2 -9 database_dump.sql
# ========== Decompress File ==========
# Basic decompression
bunzip2 data.txt.bz2
# Keep compressed file
bunzip2 -k data.txt.bz2
# ========== View Compressed File ==========
# View contents
bzcat data.txt.bz2
# Search contents
bzgrep "pattern" data.txt.bz2
4. xz/unxz – Extreme Compression Tool
Uses:
- Very high compression ratio (30%-50% higher than gzip)
- Common format for Linux distributions (e.g.,
<span>.tar.xz</span>) - Very slow compression speed, suitable for long-term storage
Basic Syntax:
# Compress file
xz [options] filename
# Decompress file
unxz [options] filename.xz
# or
xz -d filename.xz
Common Command Examples:
# ========== Compress File ==========
# Basic compression
xz large_file.iso
# Keep original file
xz -k database.sql
# Highest compression level (very slow)
xz -9 -e archive.tar
# Fast compression
xz -0 temp.log
# ========== Decompress File ==========
# Basic decompression
unxz file.xz
# Keep compressed file
unxz -k file.xz
# ========== View Compressed File ==========
# View contents
xzcat file.xz
# View compression information
xz -l file.xz
5. zip/unzip – Cross-Platform Compression Tool ⭐
Uses:
- File exchange between Windows and Linux
- Supports encrypted compression
- Can directly update files within the compressed package
- Supports split compression
Basic Syntax:
# Compress file
zip [options] target_file.zip source_file/directory
# Decompress file
unzip [options] file.zip [-d target_directory]
Common Options:
<span>-r</span>: Recursively compress directory<span>-e</span>: Encrypted compression<span>-s</span>: Split compression<span>-q</span>: Quiet mode<span>-d</span>: Specify extraction directory<span>-l</span>: List contents
Common Command Examples:
# ========== Compress File ==========
# Compress single file
zip backup.zip file.txt
# Compress directory (recursively)
zip -r project.zip /path/to/project
# Encrypted compression
zip -r -e secure.zip sensitive_data/
# Split compression (100MB per volume)
zip -r -s 100m large_backup.zip /large/data
# Exclude files during compression
zip -r backup.zip /project -x "*.log" "node_modules/*"
# Update files in compressed package
zip -u backup.zip updated_file.txt
# Silent compression (no output)
zip -q -r backup.zip /data
# ========== Decompress File ==========
# Basic decompression
unzip backup.zip
# Decompress to specified directory
unzip backup.zip -d /target/directory
# Decompress without overwriting existing files
unzip -n backup.zip
# Overwrite existing files (without prompt)
unzip -o backup.zip
# Decompress single file
unzip backup.zip path/to/file.txt
# Exclude files during decompression
unzip backup.zip -x "*.log"
# ========== View Compressed Package ==========
# List contents of compressed package
unzip -l backup.zip
# Detailed listing (including permissions)
unzip -v backup.zip
# Test integrity of compressed package
unzip -t backup.zip
6. 7z – Extreme Compression Tool
Uses:
- Compression ratio close to the limit
- Supports multiple compression formats
- Supports encryption and split
Installation:
# Debian/Ubuntu
sudo apt install p7zip-full
# RHEL/CentOS
sudo yum install p7zip p7zip-plugins
# Arch Linux
sudo pacman -S p7zip
Common Command Examples:
# ========== Compress File ==========
# Create 7z compressed package
7z a backup.7z /path/to/directory
# Extreme compression (highest level)
7z a -mx=9 backup.7z /data
# Encrypted compression
7z a -p backup.7z sensitive_data/
# Split compression (100MB per volume)
7z a -v100m backup.7z /large/data
# ========== Decompress File ==========
# Decompress 7z file
7z x backup.7z
# Decompress to specified directory
7z x backup.7z -o/target/directory
# Decompress without retaining directory structure
7z e backup.7z
# ========== View Compressed Package ==========
# List contents
7z l backup.7z
# Test integrity
7z t backup.7z
7. rar/unrar – Commercial Compression Format
Uses:
- Common format in Windows environments
- Higher compression ratio
- Requires additional software installation
Installation:
# Debian/Ubuntu
sudo apt install unrar
# RHEL/CentOS (requires EPEL repository)
sudo yum install unrar
Common Command Examples:
# ========== Compress File (requires commercial version of rar) ==========
# Create rar compressed package
rar a backup.rar /path/to/directory
# Split compression (100MB per volume)
ar a -v100m backup.rar /data
# ========== Decompress File ==========
# Decompress rar file
unrar x backup.rar
# Decompress to specified directory
unrar x backup.rar /target/directory
# Decompress without retaining directory structure
unrar e backup.rar
# ========== View Compressed Package ==========
# List contents
unrar l backup.rar
# Test integrity
unrar t backup.rar
🛠️ Engineering Practice Cases
Case 1: Automated Backup Script
Function: Scheduled backup of specified directories, generating timestamped compressed packages, verifying integrity, and automatically cleaning up expired backups.
Core Commands:
# Create a timestamped backup
tar -czvf backup_$(date +%Y%m%d_%H%M%S).tar.gz /home/user /etc /var/www
# Verify backup integrity
tar -tzf backup.tar.gz > /dev/null && echo "Backup is intact"
# Generate MD5 checksum
md5sum backup.tar.gz > backup.tar.gz.md5
# Clean up backups older than 7 days
find /backup -name "backup_*.tar.gz" -mtime +7 -delete
Case 2: Batch Compression of Log Files
Function: Automatically compress log files older than a specified number of days to free up disk space.
Core Commands:
# Compress logs older than 7 days
find /var/log/app -name "*.log" -mtime +7 -exec gzip {} \;
# Recursively compress entire log directory
gzip -r /var/log/old_logs/
Case 3: Cross-Server Transfer of Compressed Files
Function: Compress and transfer simultaneously to avoid local disk space shortages and improve transfer efficiency.
Core Commands:
# Compress and transfer to remote server
tar -czf - /data | ssh user@remote "cat > /backup/data.tar.gz"
# Compress transfer and extract on remote
tar -czf - /data | ssh user@remote "tar -xzf - -C /restore"
# Use rsync for compressed transfer
rsync -avz /data/ user@remote:/backup/
Case 4: Encrypted Backup of Sensitive Data
Function: Encrypt and compress sensitive data to ensure secure transfer and storage.
Core Commands:
# Use tar + gpg for encryption
tar -czf - /sensitive/data | gpg -c > backup_encrypted.tar.gz.gpg
# Decrypt and extract
gpg -d backup_encrypted.tar.gz.gpg | tar -xzf -
# Use zip for password encryption
zip -r -e secure_backup.zip /sensitive/data
Case 5: Compression Performance Comparison Test
Function: Compare the speed and compression ratios of different compression tools to select the optimal solution for practical scenarios.
Core Commands:
# Test gzip compression
time tar -czf test_gz.tar.gz /test/data && du -h test_gz.tar.gz
# Test bzip2 compression
time tar -cjf test_bz2.tar.bz2 /test/data && du -h test_bz2.tar.bz2
# Test xz compression
time tar -cJf test_xz.tar.xz /test/data && du -h test_xz.tar.xz
# Test 7z extreme compression
time 7z a -mx=9 test.7z /test/data && du -h test.7z
📊 Scenario Command Quick Reference Table
| Scenario | Recommended Tool | Command Example |
|---|---|---|
| DailyBackup | tar.gz | <span><span>tar -czvf backup.tar.gz /data</span></span> |
| Extreme Compression | tar.xz / 7z | <span><span>tar -cJvf backup.tar.xz /data</span></span> |
| Fast Compression | gzip -1 | <span><span>tar -czf backup.tar.gz --use-compress-program="gzip -1" /data</span></span> |
| Cross-Platform Sharing | zip | <span><span>zip -r share.zip /project</span></span> |
| Encrypted Compression | zip -e / gpg | <span><span>zip -r -e secure.zip /data</span></span> |
| Single File Compression | gzip | <span><span>gzip -k file.log</span></span> |
| Log Archiving | gzip | <span><span>find /var/log -name "*.log" -mtime +7 -exec gzip {} \;</span></span> |
| Split Compression | zip -s / 7z -v | <span><span>zip -r -s 100m backup.zip /data</span></span> |
| Incremental Backup | tar –listed-incremental | <span><span>tar -czf inc.tar.gz --listed-incremental=snap.file /data</span></span> |
| Remote Backup | tar + ssh | <span><span>tar -czf - /data | ssh user@host "cat > backup.tar.gz"</span></span> |
💡 Best Practices and Tips Summary
1. Compression Level Selection Strategy
# Choose compression level based on scenario
# Temporary files - prioritize speed
tar -czf --use-compress-program="gzip -1" temp.tar.gz /tmp/data
# Regular backups - balanced mode (default -6)
tar -czf backup.tar.gz /data
# Long-term storage - maximum compression
tar -cJf archive.tar.xz /data
2. Integrity Verification
# Create checksum file
tar -czf backup.tar.gz /data
md5sum backup.tar.gz > backup.tar.gz.md5
sha256sum backup.tar.gz > backup.tar.gz.sha256
# Verify integrity
md5sum -c backup.tar.gz.md5
sha256sum -c backup.tar.gz.sha256
3. Estimate Size Before Compression
# View uncompressed size
du -sh /data
# Estimate compressed size (gzip about 30%-50%)
# Actual compression ratio depends on file type
4. Resolve Chinese Filename Encoding Issues
# Zip files created in Windows may have encoding issues in Linux
unzip -O cp936 windows_file.zip
# Create zip for Windows from Linux (specify encoding)
zip -r -O gbk for_windows.zip /data
5. Various Ways to Exclude Files
# Method 1: Use --exclude
tar -czf backup.tar.gz --exclude='*.log' --exclude='node_modules' /project
# Method 2: Use exclude file list
cat > exclude.txt << EOF
*.log
node_modules
.git
EOF
tar -czf backup.tar.gz -X exclude.txt /project
# Method 3: Combine with find command
tar -czf backup.tar.gz $(find /project -not -name "*.log")
6. Tips for Handling Large Files
# Show compression progress (requires pv)
tar -czf - /large/data | pv > backup.tar.gz
# Limit compression speed (to avoid I/O contention)
tar -czf - /data | pv -L 10m > backup.tar.gz # Limit to 10MB/s
# Parallel compression (requires pigz)
tar -cf - /data | pigz -p 4 > backup.tar.gz # Use 4 cores
7. View Compressed Package Without Extraction
# tar series
tar -tzf file.tar.gz # List contents
tar -tzf file.tar.gz | wc -l # Count number of files
tar -xzf file.tar.gz file.txt --to-stdout # Output single file
# zip
unzip -l file.zip # List contents
unzip -p file.zip file.txt # Output single file
# 7z
7z l file.7z # List contents
⚠️ Common Traps and Precautions
1. Tar Bomb
# Problem: Files scatter in the current directory after extraction
# Solution: Check contents first, then decide whether to use -C to specify directory
tar -tzf suspicious.tar.gz | head
tar -xzf suspicious.tar.gz -C /safe/directory
2. Original File Deleted
# gzip/bzip2/xz will delete original files by default
# Solution: Use -k to keep original files
gzip -k important.log
3. Permission Issues
# Non-root users may have permission issues extracting archives created by root
# Solution: Use --no-same-owner
tar -xzf backup.tar.gz --no-same-owner
4. Insufficient Disk Space
# Compressing large files may run out of disk space
# Solution: Check available space first
df -h
# or directly transfer to another location
tar -czf - /data | ssh user@remote "cat > /backup/data.tar.gz"
📂 Official Source Code and Documentation
tar
- Source Code: GNU Tar – Git
- Documentation: GNU Tar Manual
- Man Page:
<span>man tar</span>
gzip / bzip2 / xz
- gzip Source Code: gzip GitHub
- bzip2 Source Code: bzip2 GitLab
- xz Source Code: XZ Utils
- Documentation:
<span>man gzip</span>/<span>man bzip2</span>/<span>man xz</span>
zip / unzip
- Info-ZIP Source Code: Info-ZIP
- Documentation:
<span>man zip</span>/<span>man unzip</span>
7z
- Source Code: 7-Zip Official
- p7zip (Linux version): p7zip GitHub
- Documentation:
<span>man 7z</span>
rar
- Official Website: WinRAR/RAR
- Documentation:
<span>man unrar</span>
✅ Summary
Compression and decompression are fundamental skills for Linux engineers, and mastering the characteristics and applicable scenarios of each tool is crucial:
| Tool | Recommendation Rating | Core Advantages |
|---|---|---|
| tar + gzip | ⭐⭐⭐⭐⭐ | Best balance, most widely used |
| zip | ⭐⭐⭐⭐⭐ | Best cross-platform compatibility |
| tar + xz | ⭐⭐⭐⭐ | Very high compression ratio, suitable for long-term storage |
| gzip | ⭐⭐⭐⭐ | Fast compression of single files |
| 7z | ⭐⭐⭐ | Extreme compression, commonly used in Windows |
Core Principles:
- ⚡ Speed Priority →
<span>gzip -1</span>or<span>tar.gz</span> - 💾 Space Priority →
<span>tar.xz</span>or<span>7z</span> - 🌍 Compatibility Priority →
<span>zip</span> - 🔐 Security Priority →
<span>zip -e</span>or<span>gpg</span>encryption
By mastering the content of this article, you will be able to efficiently handle compression and decompression tasks in various scenarios, improving system management efficiency. Practice is the best teacher, and it is recommended to try various command combinations in a test environment to find the solution that best fits your work scenario.
📎 Related Materials Access
The complete command quick reference table, automated script templates, compression tool performance comparison data, and other materials mentioned in this article can be obtained through the following methods:
Access Method: Follow the public account and reply in the background with “Compression Commands” or “tar”
👉 If you have any questions or practical experiences related to compression and decompression, feel free to share and discuss in the comments section!