Session and utilities

The xz command

Compresses into the .xz format: smaller files, at the cost of slower compression.

Syntax

xz [-d] [-k] archivoTry it in the terminal →

Most useful options

-d, --decompressDecompresses. Same as unxz. After success it deletes the original unless you use -k
-k, --keepKeeps the original file. Without it, xz deletes the original after a successful run
-c, --stdoutWrites the result to standard output instead of a file. Implies -k
-t, --testChecks integrity without writing anything to disk or removing files
-l, --listShows information about the compressed file: sizes, ratio and check type
-0 ... -9Compression level. The default is -6. Higher levels use more memory when compressing and decompressing
-T, --threads <n>Uses several threads; with 0, as many as the system processors. The output differs from single-thread mode

Copy-ready examples

$ printf 'texto\n' > f.txt && xz -k f.txt && lscreates a file, compresses it and keeps the original. Then lists the folder.

Example output

f.txt  f.txt.xz
$ xz -l f.txt.xzsummary of the compressed file: sizes, ratio and check type.

Example output

Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       1         72 B          6 B    ---  CRC64   f.txt.xz
$ xz -t f.txt.xz && echo integrochecks that the file is not damaged. No message if everything is fine.

Example output

integro
$ xz -9 -T 0 -c datos.csv > datos.csv.xzmaximum compression using all threads, into a new file. The original stays where it is.
$ xz -dc f.txt.xz | wc -cdecompresses into a pipe and counts the bytes. The file does not change.

Example output

6

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
xz: f.txt.xz: File existsThe target file already exists. xz does not overwrite it without -f, so the previous one is kept.Rename or delete the old .xz, or add -f if you want to replace it.
xz: bad.xz: File format not recognizedThe file is not in xz format. It may be text with a .xz extension or an incomplete download.Check its type with file bad.xz. If it is not xz, do not decompress it with xz.
xz: nonexist: No such file or directoryThe name does not exist in the current folder or is misspelled.List the folder with ls and copy the exact name.

Typical mistakes

  • xz deletes the original after a successful run. If you want to keep it, add -k.
  • Compressing at high levels is slow and needs a lot of memory. For files that change often, gzip is usually more practical.
  • A .xz file cannot be read as text directly: to view it, use xzcat or xz -dc.

Differences from similar commands

  • gzip: uses the .gz format. According to the xz manual, -0 can be faster than gzip -9 while compressing much better.
  • bzip2: also compresses a single stream. The xz manual says its higher levels run at a speed similar to bzip2, with comparable or better compression.
  • tar: bundles several files into one. xz only compresses one stream; for several files, use tar -cJf target.tar.xz folder/.

Related commands

FAQ

How much memory does decompressing need?

It depends on the level used to compress: a file made with -9 needs more memory than one made with -6. If your machine has little memory, compress at a lower level.

Can I compress a whole folder into one .xz?

Not directly: xz works on files one at a time. Bundle the folder with tar -cJf, which calls xz internally, and you get a single .tar.xz.

What is xz -t for?

To check integrity without writing anything. If it finishes with no message and exit code 0, the file is fine.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course