Introduction
In the Linux world, file compression is a daily task that allows saving storage space and accelerating data transfer. Among the available tools, the commandxzstands out for its high compression relationship and its efficiency in the use of CPU and memory.
What's xz?
xz is a compression utility based on the LZMA2 algorithm, which is part of the XZ Utils project. It was designed to provide superior compression against gzip and bzip2, maintaining a reasonable decompression rate.
Advantages of xz
- Very high compression ratio, often 20-30% better than gzip and 10-15% better than bzip2.
- Low memory consumption during compression, configurable as needed.
- Competitive decompression speed, suitable for frequent use.
- Support for .tar.xz files, allowing to pack and compress in one step.
Installation
In most modern distributions, xz comes pre-installed. If missing, it can be installed with the package manager:
- Debian / Ubuntu:
sudo apt-get install xz-utils - Fedora:
sudo dnf install xz - Arch Linux:
sudo pacman -S xz
Basic use
To compress a file:
xz -z archivo.txt
This generatesarchivo.txt.xzand delete the original by default. To decompress:
xz -d archivo.txt.xz
Or use the variantunxz:
unxz archivo.txt.xz
Advanced options
-eor--extreme: increases compression effort, achieving smaller sizes at longer cost and CPU.-T0: allows xz to use all available threads for parallel compression.-9a-0: adjusts the compression level (9 is the strongest, 0 the fastest).--verbose: shows detailed information on the process.
Comparison with gzip and bzip2
In typical tests of text and source code files, xz reaches a size reduction of 30% to 50% compared to the original file, while gzip usually leaves between 10% and 20% and bzip2 between 20% and 35%. In terms of speed, xz compression is slower than gzip but comparable to bzip2; decompression, however, is almost as fast as gzip compression.
Good practices
- Use the extension
.tar.xzfor directories backups:tar -cJf copia.tar.xz directorio/. - For incremental backups, combine xz with tools like
rsyncorborgthat handle the deduplication before compression. - Adjust the compression level according to the production environment: on high-load servers, prefer average levels (
-6a-8) to avoid CPU peaks. - Verify the integrity of the compressed files with
xz -t archivo.xzbefore you take them out.
Conclusion
The commandxzhas been consolidated as a powerful option for those who need maximum compression without sacrificing too much performance. Its flexibility, combined with the ability to use multiple cores, makes it ideal for archiving, software distribution and backup tasks in Linux systems.


