Command Overview
xz is a lossless data compression utility that provides excellent compression ratios, often better than gzip or bzip2. unxz decompresses .xz files and is equivalent to xz -d. Both commands use the LZMA2 compression algorithm, making them ideal for distributing software packages and archiving large files.
Compression Comparison
| Tool |
Extension |
Compression Ratio |
Speed |
Memory Usage |
| gzip |
.gz |
Good |
Fast |
Low |
| bzip2 |
.bz2 |
Better |
Slower |
Medium |
| xz |
.xz |
Best |
Slowest |
High |
| zstd |
.zst |
Very Good |
Very Fast |
Low-Medium |
Example 1: Basic File Compression
xz largefile.txt
$ ls -lh
-rw-r--r-- 1 user user 2.1M Nov 11 14:30 largefile.txt.xz
Compresses largefile.txt and replaces it with largefile.txt.xz. Original file is deleted by default.
- Default behavior: Removes original file after compression
- .xz extension: Automatically added
- Compression: Uses default compression level (6)
- Use case: Archive large log files, backups
Example 2: Keep Original File
xz -k document.pdf
$ ls
document.pdf document.pdf.xz
The -k option keeps the original file after compression.
- -k flag: Keep original file
- Both files exist: Original and compressed version
- Use case: When you need to keep the original
Example 3: Basic Decompression
unxz archive.tar.xz
$ ls
archive.tar
Decompresses archive.tar.xz to archive.tar, removing the .xz file.
- unxz command: Equivalent to
xz -d
- Original removed: .xz file deleted after extraction
- Use case: Extract downloaded software packages
Example 4: Maximum Compression
xz -9 -k bigfile.log
$ ls -lh
-rw-r--r-- 1 user user 100M Nov 11 14:30 bigfile.log
-rw-r--r-- 1 user user 15M Nov 11 14:35 bigfile.log.xz
Uses maximum compression level (9) for best compression ratio.
- -9 flag: Maximum compression (slowest)
- Levels 0-9: 0=fastest/least compression, 9=slowest/best compression
- Trade-off: Much slower but smaller file size
- Use case: Long-term archival, bandwidth-constrained transfers
Example 5: Fast Compression
xz -1 -k video.mp4
$ time xz -1 -k video.mp4
real 0m5.234s
$ time xz -9 -k video.mp4
real 2m15.678s
Level 1 compression is much faster but with lower compression ratio.
- -1 flag: Fastest compression
- Speed difference: 5 seconds vs 2+ minutes
- Use case: Quick compression when time matters more than size
Example 6: Compress Multiple Files
xz *.log
$ ls *.xz
app.log.xz error.log.xz access.log.xz
Compresses all .log files in current directory. Each file gets its own .xz archive.
- Wildcard: Processes multiple files
- Individual archives: Each file compressed separately
- Note: Not a single archive like tar
- Use case: Compress log rotation files
Example 7: Test Compressed File Integrity
xz -t archive.tar.xz
$ xz -t archive.tar.xz
$ echo $?
0
Tests the integrity of compressed file without extracting it.
- -t flag: Test mode
- Exit code 0: File is intact
- Non-zero: File is corrupted
- Use case: Verify downloads, check backup integrity
Example 8: View Compressed File Info
xz -l database.sql.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 45.2 MiB 234.5 MiB 0.193 CRC64 database.sql.xz
Lists detailed information about compressed file.
- -l flag: List mode
- Compression ratio: 0.193 means 19.3% of original size
- Check type: Integrity check method used
- Use case: Estimate extraction size, verify compression effectiveness
Example 9: Pipe Compression (stdout)
tar cf - mydir/ | xz > mydir.tar.xz
$ ls -lh mydir.tar.xz
-rw-r--r-- 1 user user 12M Nov 11 14:30 mydir.tar.xz
Combines tar and xz using pipes for efficient archiving.
- tar cf -: Create tar to stdout
- Pipe |: Send to xz
- Single operation: No intermediate files
- Alternative:
tar cJf mydir.tar.xz mydir/
Example 10: Decompress to stdout and View
xz -dc log.txt.xz | less
[Contents of log.txt displayed in less pager]
Decompresses to stdout without creating file, piping to less for viewing.
- -d flag: Decompress
- -c flag: Write to stdout
- No file created: Output goes to pipe
- Use case: Quick inspection without extraction
Additional Information
Common Options
-z, --compress - Force compression (default)
-d, --decompress - Decompress (same as unxz)
-k, --keep - Keep input files
-f, --force - Overwrite output files
-c, --stdout - Write to standard output
-t, --test - Test compressed file integrity
-l, --list - List compression information
-0 to -9 - Compression level (0=fast, 9=best)
-v, --verbose - Verbose mode
-q, --quiet - Suppress warnings
-T, --threads - Number of threads to use
-e, --extreme - Even better compression (slower)
Common Tar + XZ Combinations
# Create .tar.xz archive
tar cJf archive.tar.xz directory/
# Extract .tar.xz archive
tar xJf archive.tar.xz
# List contents without extracting
tar tJf archive.tar.xz
# Create with maximum compression
XZ_OPT=-9 tar cJf archive.tar.xz directory/
Related Commands
xzcat - Decompress to stdout (same as xz -dc)
xzless - View compressed file with less
xzmore - View compressed file with more
xzdiff - Compare compressed files
xzgrep - Search compressed files
gzip/gunzip - Alternative compression (faster, less compression)
bzip2/bunzip2 - Alternative compression (middle ground)
zstd - Modern compression (good balance)
Performance Tips
- Use
-T0 for multi-threaded compression (uses all CPU cores)
- Level 6 (default) offers best speed/compression balance
- Use
-e for extreme mode only when size is critical
- For quick compression, use level 1 or 2
- Consider zstd for better compression speed with similar ratios
Memory Usage by Compression Level
Compression Memory Requirements:
- Level 0-3: ~10-50 MB
- Level 4-6: ~50-100 MB
- Level 7-9: ~100-700 MB
- Decompression: ~10-50 MB (regardless of compression level)
High compression levels require significant RAM. Use lower levels on memory-constrained systems.
Pro Tip: For maximum compression with multi-threading:
xz -9e -T0 -k large_file.iso
This uses extreme compression (-9e) with all available CPU cores (-T0) while keeping the original (-k).
When to Use XZ
- Best for: Long-term archives, software distribution, bandwidth-limited transfers
- Avoid for: Already compressed files (images, videos), time-critical operations
- Consider alternatives: gzip for speed, zstd for balance