xz Command

The xz command in Linux is a general-purpose command-line data compressor that uses the LZMA2 compression algorithm. It is primarily used for compressing or decompressing .xz files, offering very high compression ratios, often better than gzip or bzip2.

Syntax

xz [OPTION]... [FILE]...

Description

xz compresses or decompresses named files. If no files are given, it compresses or decompresses from standard input to standard output. It typically replaces the original file with the compressed or decompressed version.

Common uses include:

  • Compressing large files for storage or transfer
  • Decompressing software packages or archives
  • Creating highly compressed backups
  • Working with tar archives (e.g., .tar.xz)

Common Options

Option Description
-d, --decompress, --uncompress Decompress.
-z, --compress Compress. This is the default operation.
-k, --keep Keep (don't delete) input files during compression or decompression.
-f, --force Force operation. When compressing, overwrite existing output files.
-v, --verbose Be verbose. Show the filename and percentage progress.
-T NUM, --threads=NUM Use `NUM` threads for multi-core compression.
-l, --list List information about .xz files.

Examples

Compress a file

xz myfile.txt

Compresses 'myfile.txt' into 'myfile.txt.xz' and deletes the original file.

Decompress a .xz file

xz -d myfile.txt.xz

Decompresses 'myfile.txt.xz' back to 'myfile.txt' and deletes the compressed file.

Compress and keep original file

xz -k anotherfile.log

Compresses 'anotherfile.log' into 'anotherfile.log.xz' but keeps the original file.

List contents of a .xz file

xz -l archive.tar.xz

Lists information about the compressed file, such as compressed size, uncompressed size, and compression ratio.

Decompress to standard output

xz -dc myfile.txt.xz > decompressed_output.txt

Decompresses 'myfile.txt.xz' and writes the output to 'decompressed_output.txt' without deleting the original .xz file.

See also