Open In App

XZ (Lossless Data Compression) Tool in Linux with Examples

Last Updated : 28 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

xz is a general-purpose data compression and decompression command-line tool 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.

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

Decompressing file 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads