Open In App

XZ (Lossless Data Compression) Tool in Linux with Examples

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

xz is a general-purpose data compression and decompression command-line tool. It is similar to gzip and bzip2. It can be used to compress and decompress the files. The native file format of xz is .xz. But it can also support other various formats to compress or decompress files.xz gives us complete control over the compression and decompression of files. In Linux, xz tool is come by default with the system you don't need to install it. Now let's see how to use the xz tool to compress the files.

What is xz in Linux?

`xz` is a data compression tool in Linux. It uses the LZMA2 compression algorithm to achieve high compression ratios. It is commonly used for compressing files to save space and for packaging software distributions due to its efficiency and effectiveness. The `xz` command can able to compress, decompress, and test files providing the versatility of utility management for compressed data in various scenarios.

Why use xz?

The following are the some of the reasons and insights on the use of linux xz:

  • High Compression Ratio:`xz` provides feature of superior compression compared to many other tools, significantly reducing file sizes.
  • Efficient Storage: It is Ideal for saving disk space, especially when storing or archiving large files.
  • Software Distribution: It is mostly used in packaging the software distributions due to its effective compression capabilities.
  • Versatile Utility: It supports compressing, decompressing, and testing of files, making it a comprehensive tool for file management.
  • Open Source: `xz` is freely available and open source that facilitates with ensuring the accessibility and community support.

How to do Compressing of files using xz?

  • There are two ways in which we can compress the files using the xz one way is just mention file name followed by xz command like:
xz filename

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

  • Another way is by using the -z or --compress option
xz -z filename
  • Here in place of filename mention the file name or folder name. But while compressing files using xz make sure that there is no compressed file of the same file which we are going to compress.

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

  • We can see that the file extension of the file is a change to .xz and the size of the file also gets reduced. Now let's see how to decompress the file using xz tool.

How to do Decompressing of files with xz?

  • We can decompress the compressed file by using the xz by two ways. One is using the -d option. As follows:
xz -d filename

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

  • And another way is to by using unxz command as follows:
unxz filename

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

  • We can see that in the above outputs the original file gets replaced by the compressed file. What if we want to compress the file without deleting the original file.
  • Now let's see how to create a compressed file without deleting the original file.

Creating compressed file without deleting an original file using xz

  • We can compress the file in xz without deleting the original file by using the -k option. As follows:
xz -k filename

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.

Controlling the size of compression in xz

We can control the size of compressing the file using the xz .xz provides the options for compression level between 0 and 9 the default value of compression level is 6. Here the 0 is the fast but less compressed file, and we can use the --fast option for the 0 levels. And the 9 is slow more compress the file, and we can use the --best option for level 9.

  • Here is one example:
xz -8 filename

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

  • What if your system has very little ram like 512 MB or 1 GB and you want to compress the file of size 10 GB.
  • Then we know that  to compress the huge file we need the more ram.
  • Then how to compress the huge file on a system having small ram size. Now let's see how to do that.

Reducing Ram Usage

We can reduce the memory usage using the --memlimit-compress option provided by the xz. Now let's see how to use --memlimit-compress option. To use the --memlimit-compress option u mention percentage how many percent of system ram use is allocated to the xz with the --memlimit-compress like:

xz --memlimit-compress=20% filename

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

  • Getting bored while completing the process or you want to monitor the progress of compressing or decompressing ?.Now let's see how to watch the progress of compressing or decompressing.

Progress Monitor While compressing and Decompressing:

To monitor the progress of current compressing or decompressing operation xz provide the verbose mode we can enable the verbose mode by using the -v options like.

xz -v filename

Here is example:

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

  • That's it about the xz tool. But if you want to know more about the xz tool then you can read Manuel page of xz using man command as follows:
man xz

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

Exmaples of XZ Command

The following are the examples of XZ Command:

1. Compress a File

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

2. Decompress a File

  • The following is the command used to decompress file.txt.tz to file.txt
xz -d file.txt.xz

3. compress the Maximum Compression

  • The following is the command used to compress the maximum compression:
xz -9 file.txt

4. Keep Original file after Compression

The following is the command used to compress and keep the original file:

xz -k file.txt

Best Practices of XZ Command

The following are the best practices of XZ Command:

  • Use appropriate Compression levels: Try on use the balanced compressed levels (1-9) based on your need for speed versus file size reduction.
  • Preserve Original Files: It used for -k option for keeping the original files intacting with during compression.
  • Verify Compressed Files: It always helps with integrity of compressed files using -t option.
  • Automate and Script: It is used to integrate the xz commands into scripts for batch processing and automation with ensuring consistent compression of workflow.

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.

Next Article
Article Tags :

Similar Reads