xz for Beginners : How to use xz to Compress Files in Linux/Unix Based Systems

xz is a general purpose data compression tool which uses LZMA/LZMA2 compression algorithms to compress files. This algorithm has a greater compression ratio than gzip and bzip2, which makes it a great format when we need to store data on limited disk space. But it takes significantly longer to do the compression. For instance, with the heavy compression flags on a fairly large file, gzip may require around half a minute, bzip2 might be somewhere around a minute, and xz may take around four or five minutes, but the decompression time is relatively good. It creates smaller files. This compression tool was first released in 2009, and the compressed file xz has extension of '.xz'.

The xz format is widely used for distributing software. But because it is recently developed, thus it might not be supported on some older systems. Now lets take a look of some basic use of xz to compress and/or decompress files :

First check that the xz is installed or not on your system :
 $ which xz
If xz is installed on your system then it will return the PATH of wget /usr/bin/xz. or if it not installed then nothing will return. now to install xz use the below command
 $ sudo apt-get install
 $ sudo apt-get install xz-utils
1. Compress a file

To compress a file in xz use the below command :
 $ xz [file-name]
example :
 $ xz document.txt
At here the compressed file will replace the original file. To also keep the original file, just use the -k option :
 $ xz -k document.txt
2. Compress multiple files :

To compress multiple files at once just provide the file names to xz
 $ xz [file1] [file2] [file3]
example :
 $ xz file1.txt file2.txt file3.txt
3. Verbose option :

To see the details of compression use -v option :
 $ xz -v [file-name]
example :
 $ xz -v document.txt
4. Custom name for compressed files :

To compress files with custom names use -c option :
 $ xz -c [file-name] > [compressed-file-name]
example :
 $ xz -c document.txt > docfile.txt.xz
5. To Decompress files :

To decompress a file with in xz, use -d option :
 $ xz -d [compressed-file-name]
example :
 $ xz -d document.txt.xz
and if you want to keep the compress file then put -k option :
 $ xz -dk document.txt.xz
There's also another utility called unxz which perform the decompression operation :
 $ unxz  [compressed-file-name]
6. Integrity test :

With -t option we can check the integrity of a compressed file :
 $ xz -t [compressed-file-name]
example :
 $ xz -t document.txt.xz
If the file is a valid file then no output will be returned but if the file is not valid you will receive error messages. We can also add -v option at here, then for valid files it returns OK message.
 $ xz -tv document.txt.xz
7. List compression information

With -l option, we can see the various information about compressed file :
 $ xz -l [compressed-file-name]
8. Change the compression level and compression speed :

In xz the compression speed is in between 0 to 9. At -0 the compression speed is fastest and at -9 it take a very long time but may show some gains in file compression. The -6 is the default and is a good middle ground for most use cases. So for fast file compression.
 $ xz -0 [file-name]
And for good level compression but slow compression speed
 $ xz -9 [file-name]

There's also another option '-e' for extreme level compression in situations where compression time is not important.
 $ xz -e -9 [file-name]
And at last for main page type :
 $ main xz

Conclusion :

The xz is very powerful tool to compress and/or decompress files. In this post we saw some basic usage of xz command with examples. For more information about xz you can check mannual page at here : https://linux.die.net/man/1/xz