Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Archives and Compression

Linux xz command

Reference for the Linux xz and unxz commands: compress with LZMA2, pick presets 0-9, use -e and -T for threads, test files, list sizes and create tar.xz archives.

xz compresses files with the LZMA2 algorithm and gives some of the smallest output among the common command-line compressors. Linux kernel sources, many distribution packages and software releases are shipped as .tar.xz for that reason. The price is higher CPU time and memory during compression; decompression is fairly quick.

xz file creates file.xz and removes the original, unxz (or xz -d) reverses it, and xzcat writes decompressed data to standard output. For directories combine it with tar: tar -cJf dir.tar.xz dir.

Terminal
xz data.csv             # data.csv.xz
unxz data.csv.xz

Try it

Bash Compress, test and list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Presets and streaming

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
xz [OPTION]... [FILE]...
unxz [OPTION]... [FILE.xz]...
xzcat [FILE.xz]...
Option Description
-z, --compress Compress (default).
-d, --decompress Decompress (same as unxz).
-k, --keep Keep input files.
-c, --stdout Write to standard output.
-0 ... -9 Preset; default -6. -0 is fastest; -9 the smallest and needs most memory.
-e, --extreme Spend more time to squeeze a little more at the same preset.
-T N, --threads=N Use N threads (-T0 = number of cores).
-t, --test Test integrity.
-l, --list List information about .xz files; -lv for more.
-F FORMAT xz, lzma or raw.
--memlimit=SIZE Refuse to use more memory than this.
-v Verbose (progress and ratio).

Returns: 0 on success, 1 on error, 2 on a warning (the file is still processed).

Usage notes

  • Memory by preset. Compression at -6 needs about 94 MiB, -9 about 674 MiB; decompression needs far less (around 9 MiB at -6, 65 MiB at -9). Check the target machine if you build for small systems.
  • Presets above 6 rarely help. For typical text the gain from -9 over -6 is small while compression time and memory grow. Use it for files that are compressed once and downloaded often.
  • Threads. With -T0 xz splits the input into blocks and uses all cores, at a slightly worse ratio. Decompression of such multi-block files can also run in parallel in recent versions.
  • Checks. Files store a CRC64 check by default (--check=crc32|crc64|sha256|none), which -t verifies.
  • Not for already compressed data, and tiny files gain nothing from the header overhead.
  • .lzma is the older predecessor format; xz -F lzma writes it only for compatibility.
  • Reproducibility. The output depends on version and threading options, so use the same version when a build needs identical bytes.
  • Replaces the input unless -k or -c is used.
  • Version note. xz 5.4 and later is the stable line; releases 5.6.0 and 5.6.1 contained a deliberately planted backdoor in the build files (CVE-2024-3094) and were quickly withdrawn. Keep your distribution's updated package.