Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

xz, unxz, xzcat

Compress files to .xz with very good compression (xz), unpack them (unxz), or read them (xzcat).

Level
Everyday Linux
Group
Archive and compress
Needs root
No
Package
xz
Also called
unxz, xzcat, xzgrep

What it does

xz usually makes the smallest files of the classic tools, but it uses more time and memory. It is used for kernel modules, software packages and long-term archives.

xz FILE makes FILE.xz and removes the original. With tar, use -J (tar -cJf).

How to write it

xz [OPTION]... [FILE]...
unxz [OPTION]... [FILE]...
xzcat [OPTION]... [FILE]...

Options 21

Most used

-z, --compress
Compress (the default).
-d, --decompress, --uncompress
Decompress (same as unxz).
-k, --keep
Keep the original file.
-c, --stdout
Write to the screen (stdout), keep the files.
-l, --list
Show information about .xz files (sizes, ratio, check type).
-t, --test
Test the file.
-f, --force
Overwrite existing files and compress links.
-0 ... -9
Compression level. Default 6. Higher = smaller and slower.
-e, --extreme
Try harder for a smaller file (more CPU time).
-T, --threads=n
Use N threads. 0 = as many as there are CPU cores.
-q, --quiet
Hide warnings (twice: hide errors too).
-v, --verbose
Show progress (twice: more).
-F, --format=fmt
File format: auto, xz, lzma, raw.
-C, --check=check
Integrity check type: none, crc32, crc64, sha256.
--memlimit=limit
Limit the memory use.
-S, --suffix=suf
Use another ending than .xz.
--single-stream
Decompress only the first stream.
--no-sparse
Do not make sparse files on decompress.
--block-size=size
Start a new block every SIZE bytes (for multi-thread).
-h, --help
Show help (-H for the long help with advanced options).
-V, --version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Compress a file

    xz big.sql

    You get big.sql.xz.

  2. 2Decompress

    unxz big.sql.xz

    Same as xz -d big.sql.xz.

  3. 3Compress fast with all CPU cores

    xz -T0 -k vm-disk.raw

    -T0 uses all cores; -k keeps the original.

  4. 4Make a tar.xz archive

    tar -cJf logs-2026.tar.xz /var/log/archive

    Good for archives you keep for a long time.

  5. 5Read without unpacking

    xzcat app.log.xz | grep -c ERROR

    Count errors in a compressed log.

  6. 6Show details of a compressed file

    xz -l app.log.xz

    Compressed size, uncompressed size, ratio and check type.

  7. 7Best possible compression

    xz -9e release-notes.tar

    Very slow but very small. Good for files you send many times.

  8. 8Check a downloaded file

    xz -t image.xz && echo OK

    Finds broken downloads.

  9. 9Look at a compressed kernel module

    ls /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/intel/ | head

    RHEL ships kernel modules as .ko.xz files.

  10. 10Unpack a cloud image

    xz -dk rhel-10-cloud.raw.xz

    Many cloud and SD-card images are delivered as .xz.

  11. 11Limit memory on a small machine

    xz --memlimit=256MiB -d huge.xz

    xz stops with an error instead of using too much RAM.

Watch out

  • xz removes the original by default; use -k.
  • High levels (-9, -e) need a lot of RAM and time.

Tips to remember

  • Size: xz < bzip2 < gzip. Speed: gzip > bzip2 > xz. In tar: z = gzip, j = bzip2, J = xz.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man xz