xz
Overview
The xz
command compresses files using the LZMA2 compression algorithm. It provides the best compression ratios among common compression tools but requires more CPU time and memory.
Syntax
xz [options] [file...]
unxz [options] [file...]
xzcat [file...]
Common Options
Option | Description |
---|---|
-c |
Write to stdout |
-d |
Decompress |
-f |
Force overwrite |
-k |
Keep original files |
-l |
List compressed file info |
-t |
Test integrity |
-v |
Verbose output |
-q |
Quiet mode |
-0 to -9 |
Compression level |
-e |
Use extreme compression |
-T threads |
Use multiple threads |
-M limit |
Memory usage limit |
Compression Levels
Level | Description | Memory Usage |
---|---|---|
-0 |
Fastest | ~3 MB |
-6 |
Default | ~94 MB |
-9 |
Best | ~674 MB |
-9e |
Extreme | ~674 MB |
Key Use Cases
- Maximum compression ratio
- Long-term archival
- Bandwidth-limited transfers
- Software distribution
- Backup optimization
Examples with Explanations
Example 1: Basic Compression
xz file.txt
Compresses file.txt to file.txt.xz and removes original
Example 2: Keep Original
xz -k file.txt
Compresses file but keeps the original
Example 3: Maximum Compression
xz -9e file.txt
Uses extreme compression for best ratio
Example 4: Multi-threaded
xz -T 4 largefile.txt
Uses 4 threads for compression
Understanding Compression
LZMA2 characteristics: - Excellent compression ratios - High memory usage - CPU intensive - Dictionary-based compression - Good for repetitive data
Common Usage Patterns
Compress to stdout:
xz -c file.txt > file.txt.xz
List file information:
xz -l file.txt.xz
Test compressed file:
xz -t file.txt.xz
Advanced Options
Option | Description |
---|---|
--check=type |
Integrity check type |
--memlimit=limit |
Memory usage limit |
--threads=num |
Number of threads |
--block-size=size |
Block size |
--extreme |
Extreme compression mode |
Performance Analysis
- Slowest compression speed
- Best compression ratios
- High memory requirements
- Multi-threading support
- Good for archival purposes
Memory Management
Set memory limit:
xz -M 100MiB file.txt
Check memory usage:
xz -l compressed.xz
Low-memory compression:
xz -0 file.txt
File Extensions
Extension | Description |
---|---|
.xz |
Standard xz |
.txz |
Tar + xz |
.tar.xz |
Tar + xz |
Integration Examples
With tar:
tar -cJf archive.tar.xz directory/
Database backup:
pg_dump database | xz -9 > backup.sql.xz
Log archival:
find /var/log -name "*.log" -mtime +30 -exec xz {} \;
Multi-threading
Auto-detect cores:
xz -T 0 file.txt
Specific thread count:
xz -T 8 largefile.txt
Memory per thread:
xz -T 4 -M 400MiB file.txt
Scripting Applications
Batch compression:
#!/bin/bash for file in *.txt; do xz -k -v "$file" echo "Compressed: $file" done
Optimal compression:
compress_optimal() { local file="$1" local cores=$(nproc) xz -9e -T "$cores" -k "$file" }
Integrity Checking
Built-in checks:
xz --check=crc64 file.txt
Verify integrity:
xz -t file.txt.xz && echo "File OK"
List check type:
xz -l file.txt.xz | grep Check
Best Practices
- Use for long-term storage
- Consider memory requirements
- Use multi-threading for large files
- Test compressed files
- Monitor system resources during compression
Comparison with Other Tools
Tool | Ratio | Speed | Memory | CPU |
---|---|---|---|---|
gzip | 3:1 | Fast | Low | Low |
bzip2 | 4:1 | Medium | Medium | Medium |
xz | 5:1 | Slow | High | High |
Troubleshooting
- Out of memory errors
- Slow compression speed
- Corrupted files
- Thread synchronization issues
- Disk space problems
Security Considerations
- Verify file integrity
- Check available resources
- Monitor compression processes
- Validate file sources
- Test decompression before deleting originals
Advanced Configuration
Custom presets:
xz --preset=6e file.txt
Block size optimization:
xz --block-size=1MiB file.txt
Dictionary size:
xz --lzma2=dict=16MiB file.txt