XZ (Lossless Data Compression) Tool in Linux with Examples
Last Updated :
03 Jul, 2024
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

- 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.

- 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

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

- 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

- 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.
xz -8 filename

- 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

- 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:

- 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

Difference between tar xz and zip
The following are the differences between tar xz and zip tools:
Feature | tar xz | zip |
---|
Compression Algorithm | It uses xz (LZMA2 algorithm) | It uses deflate algorithm |
File Handling | it combines multiple files into a single archive before compressing | It compresses files individually within a single archive |
Compression Ratio | it generally offers higher compression ratios | Typically lower compression ratios compared to xz |
Compatibility | Commonly used in Unix/Linux environments; requires additional tools on Windows | It is widely supported across all major operating systems including Windows, macOS, and Linux |
File Extension | It commonly results in .tar.xz files | It 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.
Similar Reads
How to Compress Files Faster with Pigz Tool in Linux
Pigz (parallel implementation of gzip) is a free, open-source multi-threaded compression software for Linux that compresses and uncompresses files. Pigz is pronounced as "pig-zee", it compresses data using the zlib and pthread libraries and takes full advantage of many processors and cores. Pigz can
3 min read
ZIP command in Linux with examples
In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
shred Command in Linux with Examples
When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your
6 min read
Linux stress command With Examples
The 'stress' command is a robust command-line tool available on Linux-based operating systems. It's designed to assess the performance and reliability of hardware and software by simulating a high-load environment. Here we will cover everything from installation to practical usage.Introduction to "s
5 min read
od command in Linux with example
The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to
6 min read
sync command in Linux with Examples
sync command in Linux is used to synchronize cached writes to persistent storage. If one or more files are specified, sync only them, or their containing file systems. Syntax: sync [OPTION] [FILE]... Note: Nothing is being shown in the screenshots just because sync command makes the cache in the bac
1 min read
zdiff command in Linux with Examples
The zdiff command in Linux is used to invoke the diff program on files compressed via gzip. All options specified are passed directly to diff. By utilizing "zdiff," you can easily analyze differences between compressed files without the need to decompress them beforehand. Important Points:If only on
2 min read
zgrep command in Linux with Examples
The zgrep command is used to search out expressions from a given a file even if it is compressed. All the options that applies to the grep command also applies to the zgrep command. Syntax:  zgrep [grep options] Expression File nameOptions: -c : This option is used to display the number of matching
3 min read
ranlib command in Linux with Examples
ranlib command in Linux is used to generate index to archive. ranlib generates an index to the contents of an archive and it will be stored in the archive. The index lists each symbol defined by a member of an archive which is simply a relocatable object file. You may use 'nm -s' or 'nm âprint-armap
4 min read
rsync command in Linux with Examples
rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One is the source or the local-host from which the files will be synced, the other is the remote-host, on which synchronization will take place. Ther
7 min read