The xz command in Linux: high-efficiency compressor

Introduction

In the Linux world, file compression is a daily task that allows saving storage space and speeding up data transfer. Among the available tools, the xz command stands out for its high compression ratio and efficiency in CPU and memory usage.

What is 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 compared to gzip and bzip2, while maintaining a reasonable decompression speed.

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 according to needs.
  • Competitive decompression speed, suitable for frequent use.
  • Support for .tar.xz files, allowing packing and compressing in a single step.

Installation

In most modern distributions, xz comes preinstalled. If missing, it can be installed via the package manager:

  • Debian/Ubuntu: sudo apt-get install xz-utils
  • Fedora: sudo dnf install xz
  • Arch Linux: sudo pacman -S xz

Basic Usage

To compress a file:

xz -z file.txt

This generates file.txt.xz and removes the original by default. To decompress:

xz -d file.txt.xz

Or alternatively use the unxz variant:

unxz file.txt.xz

Advanced Options

  • -e or --extreme: increases compression effort, achieving smaller sizes at the cost of more time and CPU.
  • -T0: allows xz to use all available threads for parallel compression.
  • -9 to -0: sets the compression level (9 is strongest, 0 is fastest).
  • --verbose: shows detailed information about the process.

Comparison with gzip and bzip2

In typical tests of text files and source code, xz achieves a size reduction between 30 % and 50 % relative to the original file, while gzip usually ends up between 10 % and 20 % and bzip2 between 20 % and 35 %. Regarding speed, xz compression is slower than gzip but comparable to bzip2; however, decompression is almost as fast as gzip.

Best Practices

  • Use the .tar.xz extension for directory backups: tar -cJf backup.tar.xz directory/.
  • For incremental backups, combine xz with tools like rsync or borg that handle deduplication before compression.
  • Adjust the compression level according to the production environment: on high-load servers, prefer medium levels (-6 to -8) to avoid CPU spikes.
  • Verify the integrity of compressed files with xz -t file.xz before deleting them.

Conclusion

The xz command has established itself as a powerful option for those who need maximum compression without sacrificing too much performance. Its flexibility, combined with the ability to leverage multiple cores, makes it ideal for archiving, software distribution, and backup tasks on Linux systems.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .