Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 -J uses it — you already know the flag; this page explains what is inside.
  • Resumable and scriptable — it handles huge files and single-file compression cleanly.

xz trades speed for size. Compressing a busy live log with xz is 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 through xz.

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 file deletes the input unless you pass -k.
  • Choosing xz for speed-critical work — compression is slow on large trees; for quick archives prefer gzip.
  • Forgetting -J in tar — tar -xf file.tar.xz works 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 xz with zip — unrelated tools that happen to share letters; xz compresses, zip archives.
PageRelationship
tarThe bundler -J calls; the two produce .tar.xz together
gzipThe faster, weaker default compressor
bzip2The older mid-range compressor xz replaced
zipThe cross-platform archive format, unrelated to LZMA

xz Cheatsheet

CommandWhat it does
xz fileCompress to file.xz, removing the original
xz -k fileCompress but keep the original
xz -c file > out.xzCompress to standard output
xz -9 fileCompress with the strongest setting
unxz file.xzDecompress back to the original file
xz -d file.xzSame as unxz
xz -dc file.xz | lessRead a compressed file without decompressing
xzcat file.xzSame as xz -dc
xz -t file.xzTest archive integrity
tar -cJf archive.tar.xz dir/Bundle and compress a directory with xz