Using xz Compression in Linux

xz compression is a high-ratio data compression tool widely used in Linux environments to reduce file sizes, improve transfer speeds, and save storage space. Developed by Lasse Collin and based on the LZMA (Lempel-Ziv-Markov chain-Algorithm) compression algorithm, xz offers superior compression ratios compared to traditional tools like gzip and bzip2.

How xz Compression Works

The xz algorithm breaks input data into small blocks and compresses each block independently using the LZMA algorithm. The compressed blocks are then combined and stored in an output file with the .xz extension. LZMA uses a combination of dictionary-based and statistical compression techniques to achieve exceptional compression ratios.

The xz format is a container format that supports multiple compression algorithms, including LZMA, BCJ (Branch/Call/Jump), and Delta filters for specialized data types.

Basic Usage

Installing xz Utilities

Most Linux distributions include xz utilities by default. If not installed, use your package manager

# Ubuntu/Debian
sudo apt install xz-utils

# CentOS/RHEL/Fedora
sudo yum install xz

Compressing Files

To compress a file using xz

xz filename

Example

xz example.txt

This creates example.txt.xz and removes the original file.

Decompressing Files

To decompress an xz file

unxz filename.xz
# or
xz -d filename.xz

Example

unxz example.txt.xz

Using xz with Tar Archives

The xz compression tool integrates seamlessly with tar to create compressed archives

# Create compressed tar archive
tar -Jcvf archive.tar.xz files...

# Extract compressed tar archive
tar -Jxvf archive.tar.xz

Example

# Archive current directory
tar -Jcvf backup.tar.xz *

# Extract archive
tar -Jxvf backup.tar.xz

Advanced Options

Compression Levels

xz supports compression levels from -0 (fastest) to -9 (slowest but highest ratio). Default is -6

# Maximum compression
xz -9 filename

# Fast compression
xz -1 filename

Memory and Threading Control

Control memory usage and enable multi-threading for better performance

# Limit memory to 512MB
xz --memory=512M filename

# Use 4 threads
xz -T4 filename

# Combine options
xz -9 -T4 --memory=1G filename

Pipe Operations

Use xz with pipes for streaming compression

# Compress command output
ls -la | xz > listing.txt.xz

# Decompress and view
xz -dc file.txt.xz | less

# Chain with other commands
cat data.txt | xz | ssh user@server "cat > remote_file.txt.xz"

Comparison with Other Tools

Tool Compression Ratio Speed Memory Usage Common Use
gzip Good Fast Low General purpose
bzip2 Better Moderate Moderate Better compression
xz Excellent Slow High Maximum compression

Common Use Cases

  • Software distribution Linux kernel sources and packages

  • System backups Long-term storage with maximum compression

  • Log file archival Compress old logs for space efficiency

  • Data transfer Reduce bandwidth usage over slow connections

Conclusion

xz compression provides exceptional compression ratios at the cost of slower processing and higher memory usage. It's ideal for scenarios where storage space is critical and compression time is less important. The tool integrates well with existing Linux utilities and offers flexible options for various use cases.

Updated on: 2026-03-17T09:01:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements