LinuxCommandLibrary

xz

Compress or decompress '.xz' files

TLDR

Compress a file using xz

$ xz [path/to/file]
copy

Decompress an XZ file
$ xz [[-d|--decompress]] [path/to/file.xz]
copy

Compress a file using lzma
$ xz [[-F|--format]] lzma [path/to/file]
copy

Decompress an LZMA file
$ xz [[-d|--decompress]] [[-F|--format]] lzma [path/to/file.lzma]
copy

Decompress a file and write to stdout (implies --keep)
$ xz [[-d|--decompress]] [[-c|--stdout]] [path/to/file.xz]
copy

Compress a file, but don't delete the original
$ xz [[-k|--keep]] [path/to/file]
copy

Compress a file using the fastest compression
$ xz -0 [path/to/file]
copy

Compress a file using the best compression
$ xz -9 [path/to/file]
copy

SYNOPSIS

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

PARAMETERS

-d, --decompress, --uncompress
    Decompress. This is the default if the command is invoked as unxz or xzcat.

-z, --compress
    Compress. This is the default if the command is invoked as xz.

-k, --keep
    Don't delete input files after successful compression or decompression.

-f, --force
    Force overwrite of output files and compress symbolic links. This also allows compressing or decompressing to/from a terminal.

-c, --stdout, --to-stdout
    Write output to standard output and don't delete input files. This is the default if the command is invoked as xzcat.

-0 .. -9
    Set compression preset level. -0 is fastest, -9 is smallest (highest compression). Default is -6.

-T NUM, --threads=NUM
    Use NUM threads for multi-threaded compression or decompression. Use 0 for as many threads as there are CPU cores.

-l, --list
    List information about compressed files. No files are decompressed.

-t, --test
    Test the integrity of compressed files. No files are decompressed.

-q, --quiet
    Suppress all warnings and messages.

-v, --verbose
    Be more verbose. Output information like compression ratio.

-V, --version
    Display the version number of xz and xz-utils.

-h, --help
    Display a help message and exit.

DESCRIPTION

xz is a powerful and versatile command-line utility for lossless data compression, primarily using the LZMA2 algorithm. It's designed to offer high compression ratios, often outperforming older tools like gzip and bzip2, making it ideal for distributing large files, software packages, and system archives. The tool is part of the xz-utils package and can compress files into the .xz format, as well as decompress legacy .lzma files. It offers various options for controlling compression levels, from fastest to smallest, and includes robust integrity checking features. xz seamlessly integrates into typical Unix workflows, often piped with tar for creating compressed archives. Its efficiency in reducing file sizes makes it a standard choice across many Linux distributions for package management and data distribution.

CAVEATS

While xz offers superior compression, it can be slower for both compression and decompression compared to less efficient algorithms like those used by gzip, especially at higher compression levels. High compression levels also demand significantly more RAM during the compression process. Users should also be aware of the critical supply chain vulnerability (CVE-2024-3094) discovered in early 2024 in versions 5.6.0 and 5.6.1 of the xz-utils library, which could allow unauthorized remote code execution. It is crucial to ensure your system uses patched or unaffected versions of xz-utils.

ALIASES AND INTEGRATION

xz typically creates symbolic links for common operations, allowing it to be invoked as unxz (for decompression), xzcat (for decompressing to standard output), lzma, unlzma, and lzcat (for backward compatibility with the legacy LZMA format). It is frequently integrated with the tar command for creating compressed archives, e.g., tar -Jcvf archive.tar.xz files/ to compress, or tar -Jxvf archive.tar.xz to extract.

LZMA2 ALGORITHM

The LZMA2 algorithm, which xz employs, is an improved version of the LZMA (Lempel–Ziv–Markov chain algorithm) developed by Igor Pavlov for 7-Zip. It offers variable dictionary sizes, support for multiple threads, and the ability to combine multiple LZMA streams into a single LZMA2 stream, which is particularly beneficial for compressing heterogeneous data and achieving high compression ratios efficiently.

HISTORY

xz was developed by Lasse Collin, aiming to provide a modern, high-performance compression utility based on the LZMA2 algorithm, which originated from Igor Pavlov's 7-Zip project. It first achieved stable release around 2009, quickly gaining traction as a superior alternative to gzip and bzip2 due to its significantly better compression ratios. Its adoption grew steadily, becoming the default compression method for many Linux distribution packages, including Debian, Fedora, and Arch Linux, for its efficiency in reducing file sizes. A notable recent event in its history is the discovery of the highly sophisticated supply chain backdoor (CVE-2024-3094) in March 2024, affecting xz-utils versions 5.6.0 and 5.6.1. This incident highlighted the critical importance of supply chain security in open-source projects.

SEE ALSO

gzip(1), bzip2(1), tar(1), lzma(1)

Copied to clipboard