Working with XZ (Lossless Data Compression) Tool in Linux

XZ is another general purpose data compression tool with command line syntax similar to gzip and bzip2. xz compresses or decompresses each file according to the selected operation mode. It supports different formats to compress or decompress files.

XZ Command Examples in Linux

To create a compressed file you just need to use -z or –compress flag as follows. Keep in mind that it wont keep the input file.

$  xz  -z  file
    output will be:  file.xz


If you wan to keep the input file you can make use of option -k or –keep as follows.

$ xz  -zk  file
   output will be: file.xz  and input file will be saved.


To decompress the file you can use -d option or unxz utility.

$ xz  -d  file.xz
$ unxz  file.xz


if an operation fails, for instance a compressed file with same name exists, you can use the -f option to force the process.

$  xz  -zkf  file

There is also an option to specify the level of compression to be used. The compression level varies between 0 to 9 and the default is 6.
you can also specify –fast (least compression ratio) to compress fast or –best (highest compression ratio) but slower compression.

$  xz  -k  –fast  file
$  xz   -k  –best  file

Following is an example of using tar archiving utility with xz.

$  tar  -cJf  txt_files.tar.xz   *.txt

Finally, you can check the integrity of the compressed file using -t option and you can also display information using -l option.

$  xz  -t  txt_files.tar.xz

$  xz  -l  txt_files.tar.xz

XZ is powerful and so far the best compression tools available for Linux. In this tutorial we had a look at multiple examples on how to use xz. Use to comment section below to ask any question or to provide feedback.

7 Replies to “Working with XZ (Lossless Data Compression) Tool in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *