xz Command: Tutorial & Examples

Compress or decompress files

The xz command is a command-line utility used to compress or decompress files. It uses the LZMA2 compression algorithm, which provides a high compression ratio, but at the cost of increased processing time. The basic syntax of the command is: xz [options] [file]

To compress a file, the command is used as xz [options] file, for example xz file.txt will compress the contents of file.txt and create a file named file.txt.xz.

To decompress a file, the command is used as xz -d [options] file.xz, for example xz -d file.txt.xz will decompress file.txt.xz and create a file named file.txt.

The -k option can be used to keep the original file after compression, and -c option can be used to write the output to the standard output, instead of a file.

The -z option can be used to compress with gzip/gunzip, -J option can be used to compress with bzip2/bunzip2 or -Z option can be used to compress with compress/uncompress.

The xz command is particularly useful for compressing large files, such as log files and backups, as it can significantly reduce the amount of storage space required. The compression ratio of xz is generally better than gzip and bzip2 but the CPU cost is higher.

For the best tradeoff between CPU time and compression ratio, we recommend to use zstd.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA