xz Command Guide for Maximum Compression
xz is the modern high-compression tool — it squeezes files smaller than gzip or bzip2 ever could, at the cost of slower speed, and produces the .xz files behind .tar.xz archives.
What is xz, and Why Use It?
xz compresses files with the LZMA algorithm, which achieves the best size-to-effort ratio of the common Linux compressors. It is what tar -J calls, and it is why .tar.xz releases of software are often noticeably smaller than the same software’s .tar.gz. The trade-off is time: compression and decompression are slower than gzip, so xz fits files you archive once and rarely touch — source releases, backups, datasets.
Why Learn xz?
- Smallest files of the common formats — often 20–30% smaller than
.tar.gz. - The default for modern source releases — many projects now ship
.tar.xz. tar -Juses it — you already know the flag; this page explains what is inside.- Resumable and scriptable — it handles huge files and single-file compression cleanly.
xztrades speed for size. Compressing a busy live log withxzis a bad time; compressing a finished project archive once is exactly what it is for.
1. Compress a File
xz notes.txt
This replaces notes.txt with notes.txt.xz and deletes the original. To keep the original:
xz -k notes.txt
-k— keep the input file.
Choose a compression level from 0 (fastest) to 9 (smallest):
xz -9 notes.txt
The default is -6.
2. Decompress a File
unxz notes.txt.xz
xz -d notes.txt.xz
Both restore notes.txt and remove the .xz. If you kept the original with -k, decompressing still works normally — unxz restores the .xz file back to its uncompressed form.
3. Compress Without Deleting
To write compressed output without touching the input:
xz -c notes.txt > notes-backup.xz
-c— write to standard output instead of replacing the file.
Like gzip -c, this turns xz into a filter usable in pipes.
4. Read a Compressed File Without Decompressing
xz -dc notes.txt.xz | less
xz -dc big.log.xz | grep "error"
-d— decompress.-c— write to standard output.
xzcat is the dedicated alias for this:
xzcat notes.txt.xz | head -5
5. Compress Many Files
xz *.log
Each file becomes its own .xz. To preview the work verbosely:
xz -kv *.log
6. Test Integrity
xz -t backup.tar.xz
-t— test the compressed data for corruption.
No output means the file is intact.
7. The Relationship With tar
xz and tar combine with the -J flag:
tar -cJf project.tar.xz project/
tar -xJf project.tar.xz
-J— filter the archive throughxz.
This is the same pattern as tar -czf with gzip, just with the stronger compressor. Extracting a .tar.xz is identical to extracting a .tar.gz apart from the flag letter.
Common Pitfalls
- Losing the original — like
gzip,xz filedeletes the input unless you pass-k. - Choosing
xzfor speed-critical work — compression is slow on large trees; for quick archives prefergzip. - Forgetting
-Jin tar —tar -xf file.tar.xzworks because modern tar auto-detects, but being explicit (-xJf) is clearer and always correct. - Assuming every tool reads
.xz— some older utilities only understand.gz; check the receiving system before switching formats. - Confusing
xzwithzip— unrelated tools that happen to share letters;xzcompresses,ziparchives.
Related Pages
| Page | Relationship |
|---|---|
| tar | The bundler -J calls; the two produce .tar.xz together |
| gzip | The faster, weaker default compressor |
| bzip2 | The older mid-range compressor xz replaced |
| zip | The cross-platform archive format, unrelated to LZMA |
xz Cheatsheet
| Command | What it does |
|---|---|
xz file | Compress to file.xz, removing the original |
xz -k file | Compress but keep the original |
xz -c file > out.xz | Compress to standard output |
xz -9 file | Compress with the strongest setting |
unxz file.xz | Decompress back to the original file |
xz -d file.xz | Same as unxz |
xz -dc file.xz | less | Read a compressed file without decompressing |
xzcat file.xz | Same as xz -dc |
xz -t file.xz | Test archive integrity |
tar -cJf archive.tar.xz dir/ | Bundle and compress a directory with xz |