The Wayback Machine - https://web.archive.org/web/20260224054009/https://www.geeksforgeeks.org/linux-unix/xz-lossless-data-compression-tool-in-linux-with-examples/

XZ (Lossless Data Compression) Tool in Linux

Last Updated : 13 Feb, 2026

XZ is a data compression tool in Linux that helps reduce the size of files using the efficient LZMA2 compression algorithm. It is commonly used to save space or for distributing software packages.

  • Achieves high compression ratios (smaller file sizes).
  • Useful for saving disk space and archiving large files.
  • Supports both compression and decompression.

Example: Compressing a file

The following command is used to compress the a example.txt to example.txt.xz

Command:

xz example.txt

Output:

file

Syntax:

xz [options] file_name
  • xz: The command used for compression or decompression.
  • [options]: Flags or parameters that modify the command’s behavior (e.g., -d, -k, -v).
  • [file_name]: The name of the file you want to compress or decompress.

Examples of xz Command

Here are some practical examples of using the xz command with different options in Linux.

1. Compressing files using xz

Files can be compressed using the xz utility in the following ways:

Method 1: Using the Default Command

Compression can be performed by specifying the file name directly with the xz command:

Command:

xz filename

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with ExamplesMethod 2: Using the -z or --compress Option

Alternatively, the -z (or --compress) option can be explicitly used to compress the file:

Command:

xz -z filename
  • Replace filename with the actual name of the file to be compressed.

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

Note: Ensure that a compressed version of the same file does not already exist in the directory before running the command.

  • After successful compression the file extension changes to .xz. The file size is reduced compared to the original file.

2. Decompressing Files Using xz

Compressed .xz files can be decompressed using either of the following methods:

Method 1: Using the -d Option

The -d (decompress) option with the xz command can be used to extract the compressed file

Command:

xz -d filename.xz

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

Method 2: Using the unxz Command

The unxz command can be used to decompress the file:

Command:

unxz filename.xz

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

  • After decompression the .xz file is replaced with the original uncompressed file. By default, the compressed file is removed once extraction is complete.

3. Creating a Compressed File Without Deleting the Original File Using xz

By default, the xz command removes the original file after compression. To retain the original file, use the -k (keep) option.

Command:

xz -k filename

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

  • We can see that in the above image that the size of the original file is having size is 70Mb and the size of the compressed file is 69Mb.
  • We can see that there is not so much difference in the size of the original file and compressed file. But what if we want more small compressed file ?.let's see how to can we do that.

4. Controlling the Compression Level in xz

The xz utility allows control over the compression level to balance speed and compression ratio. Compression levels range from 0 to 9, with the default level set to 6.

  • Level 0 (--fast): Provides the fastest compression with a lower compression ratio.
  • Levels 1–8: Offer a balance between speed and compression efficiency.
  • Level 9 (--best): Produces maximum compression but requires more time and system resources.

Command:

xz -8 filename

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

5. Reducing RAM Usage in xz

The xz utility provides the --memlimit-compress option to limit memory usage during compression. This is particularly useful when working on systems with limited RAM. The --memlimit-compress option allows you to specify the maximum amount of memory that xz can use, either as a percentage of total system RAM or as a fixed size.

Command:

xz --memlimit-compress=20% filename
  • xz is restricted to using 20% of the total system memory for compression.

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

6. Monitoring Progress During Compression and Decompression

The xz utility provides a verbose mode to monitor the progress of compression or decompression operations. This can be enabled using the -v option.

Command:

xz -v filename

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

Additional Information

For more detailed information about the xz utility and its options, you can consult the manual page. This will display the complete documentation for xz, including advanced options and usage examples.

Command:

man xz

Output:

Learn XZ (Lossless Data Compression Tool) in Linux with Examples

Difference between tar xz and zip

The following are the differences between tar xz and zip tools:

Featuretar xzzip
Compression AlgorithmIt uses xz (LZMA2 algorithm)It uses deflate algorithm
File Handlingit combines multiple files into a single archive before compressingIt compresses files individually within a single archive
Compression Ratioit generally offers higher compression ratiosTypically lower compression ratios compared to xz
CompatibilityCommonly used in Unix/Linux environments; requires additional tools on WindowsIt is widely supported across all major operating systems including Windows, macOS, and Linux
File ExtensionIt commonly results in .tar.xz filesIt results in .zip files

Use Cases of XZ Command

The following are the use cases of XZ Command:

  • Archiving Log Files: It useful for compressing the large files to save the disk space.
  • Distributing Software Packages: It used for reducing download times and bandwidth usage by compressing the software packages.
  • Backup and Restore: It useful for minimizing the storage needsand to improve the transfer speeds for backups.
  • Embedded Systems: It is helps for optimzing the storage and performance by compressing the frimware and application files.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment

Explore