🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

xz Command

Intermediate Compression & Archives man(1)

Compress files using LZMA2 algorithm (best compression)

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
xz [OPTION]... [FILE]...

What Does xz Do?

xz compresses files using the LZMA2 algorithm, providing the best compression ratios of the common Linux compression tools. xz-compressed files use the .xz extension.

xz produces significantly smaller files than gzip or bzip2, but at the cost of higher CPU usage and longer compression time. Decompression is relatively fast. xz is the standard for Linux kernel source distribution.

xz is ideal for archival storage, software distribution, and any case where minimizing file size is the priority.

Options & Flags

OptionDescriptionExample
-d Decompress xz -d file.xz
-k Keep original file xz -k largefile.tar
-0 to -9 Compression level (0=fast, 9=best) xz -9 archive.tar
-T Number of threads xz -T$(nproc) large_file
-v Verbose xz -v file
-c Write to stdout xz -c file > file.xz
-l List compressed file info xz -l file.xz

Practical Examples

#1 Compress a file

Compresses with best default settings. Creates large_backup.tar.xz.
$ xz large_backup.tar

#2 Parallel compression

Uses all CPU cores for faster compression.
$ xz -T$(nproc) -9 huge_file.tar

#3 Decompress

Decompresses back to archive.tar. Or use unxz.
$ xz -d archive.tar.xz

#4 Create tar.xz

Creates an xz-compressed tar archive.
$ tar cJf release.tar.xz source/

#5 Keep original

Compresses while keeping the original.
$ xz -k database.sql

#6 Check file info

Shows compressed/uncompressed sizes and compression ratio.
$ xz -l archive.tar.xz

Tips & Best Practices

Use -T for multi-threading: xz -T0 uses all available cores. This dramatically speeds up compression: xz -T0 -9 large_file.
Best compression ratio: xz typically compresses 20-30% better than gzip and 10% better than bzip2, at the cost of speed.
High memory usage: xz -9 uses significant memory (~674MB). Lower levels use less. Ensure sufficient RAM for the chosen level.

Frequently Asked Questions

When should I use xz?
When smallest file size is important — software distribution, archival storage, or bandwidth-limited transfers. For quick compression, use gzip.
How do I decompress .xz files?
xz -d file.xz or unxz file.xz. For tar.xz: tar xf file.tar.xz (auto-detects).
How do I speed up xz compression?
Use multi-threading: xz -T$(nproc) file. Or lower the level: xz -1 is much faster with decent compression.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →