xz
37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0xz compresses with LZMA2, the algorithm behind 7z. It produces some of the smallest files of any common tool, decompresses reasonably fast, and is the standard for source tarballs (.tar.xz) and many package formats. Compression is slow and memory-hungry. If you compress once and distribute widely, xz is a good choice; if you compress often, use zstd.
Common commands
Section titled “Common commands”xz file # file.xz, preset 6; deletes filexz -9e -k file # maximum compression, keep the originalxz -d file.xz # decompress (or unxz)xz -dc file.xz | lessxz -t file.xz # test integrityxz -l file.xz # sizes and ratioNote that xz deletes the input after success unless you pass -k.
Presets
Section titled “Presets”-0 … -9 select dictionary size and match-finder effort. The default is -6. Appending -e (-6e, -9e) searches harder for a little extra compression at significant extra time.
| Preset | Dictionary | Compress memory | Decompress memory |
|---|---|---|---|
-1 | 1 MiB | 9 MiB | 2 MiB |
-3 | 4 MiB | 32 MiB | 5 MiB |
-6 | 8 MiB | 94 MiB | 9 MiB |
-9 | 64 MiB | 674 MiB | 65 MiB |
A dictionary larger than the input is wasted memory, so -9 on a 5 MB file is slower than -6 for no gain. The decompressor needs memory proportional to the dictionary, which matters for small devices and installers: a -9e file needs ~65 MiB to unpack.
Since XZ Utils 5.6.0, xz compresses multithreaded by default, using all cores. It splits the input into independent blocks, which slightly reduces the ratio and multiplies memory by the thread count, but lets the file be decompressed multithreaded too. Use -T1 for the single-threaded compressor when you need the smallest possible output or memory is tight. Older versions were single-threaded unless given -T0.
Archives and pipelines
Section titled “Archives and pipelines”xz compresses one stream. Use tar for directories:
tar -cJf backup.tar.xz dir/XZ_OPT='-9e' tar -cJf backup.tar.xz dir/tar -xf backup.tar.xz-J uses the default preset; set others through XZ_OPT or -I 'xz -9e'.
Other notes
Section titled “Other notes”- Integrity check: xz stores a CRC64 by default (
--check=crc32|crc64|sha256|none).xz -tverifies it. .lzmaandlzip: the older raw LZMA format (xz --format=lzma) is obsolete. lzip is a separate format.- Not for HTTP: there is no
xzHTTP content coding. Use brotli, zstd or gzip. - Memory limit errors while decompressing only happen when a limit is set (for example
XZ_DEFAULTS=--memlimit=150MiB); xz has none by default. Raise it with--memlimit-decompress=….