Linux xz Command Tutorial for Beginners (7 Examples)
On this page
- Linux xz command
- Q1. How to use the xz command?
- Q2. How to make xz retain original file as well?
- Q3. How to compress multiple files?
- Q4. How to decompress .xz files?
- Q5. How to make xz print information about compressed files?
- Q6. How to specify a different compression/decompression format?
- Q7. How to make xz display progress indicator?
There are several ways to compress and decompress files in Linux. While we have already discussed some (here and here), there are plenty that we still haven't. So here, in this tutorial, we will discuss another such command line utility.
It's dubbed xz. We will discuss the basics of this command line tool. But before we jump into that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.
Linux xz command
As already mentioned in the beginning, the xz command in Linux lets you compress and decompress files. Following is its syntax:
xz [option...] [file...]
And here's what the man page has to say about it:
xz is a general-purpose data compression tool with command line syntax similar to gzip(1)
and bzip2(1). The native file format is the .xz format, but the legacy .lzma format used
by LZMA Utils and raw compressed streams with no container format headers are also supported.
xz compresses or decompresses each file according to the selected operation mode. If no files
are given or file is -, xz reads from standard input and writes the processed data to
standard output. xz will refuse (display an error and skip the file) to write compressed data
to standard output if it is a terminal. Similarly, xz will refuse to read compressed data
from standard input if it is a terminal.
Following are some Q&A-styled examples that should give you a better idea on how xz works.
Q1. How to use the xz command?
Basic usage is fairly straight forward. Just pass the name of the file (that's to be compressed) as an input argument to xz. Here's an example:
On my system, the aforementioned command produced the following file as output.
So you can see the xz command compressed file.txt. It's worth mentioning that the operation replaces the original file (file.txt in this case) with the compressed version.
Q2. How to make xz retain original file as well?
As I mentioned in the previous section, the xz command replaces the original file with its compressed version. However, if you want, you can force xz to retain the original file as well. This can be done using the -k command line option.
xz -k file.txt
So this time, you'll find both file.txt and file.txt.xz in the current working directory.
Q3. How to compress multiple files?
This is very simple. Just pass the names as input arguments to xz.
xz file1.txt file2.txt
This command will compress both these files in one go.
Q4. How to decompress .xz files?
To decompress .xz files, use the -d command line option. For example:
xz -d file.txt.xz
This command would produce file.txt in the current working directory.
Q5. How to make xz print information about compressed files?
This can be done using the -l command line option. For example:
xz -l file.txt.xz
This command produced the following information in my case:
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 96 B 37 B 2.595 CRC64 file.txt.xz
Q6. How to specify a different compression/decompression format?
This can be done using the -F command line option. To use this option though, you need to understand the following:
-F format, --format=format
Specify the file format to compress or decompress:
auto This is the default. When compressing, auto is equivalent to xz. When decompressing, the format of the input file
is automatically detected. Note that raw streams (created with --format=raw) cannot be auto-detected.
xz Compress to the .xz file format, or accept only .xz files when decompressing.
Compress to the legacy .lzma file format, or accept only .lzma files when decompressing. The alternative name alone
is provided for backwards compatibility with LZMA Utils.
raw Compress or uncompress a raw stream (no headers). This is meant for advanced users only. To decode raw streams, you
need use --format=raw and explicitly specify the filter chain, which normally would have been stored in the container
So you see, you can use any of the following formats: 'auto', 'xz', 'lzma', and 'raw'.
Q7. How to make xz display progress indicator?
This can be made possible using the -v command line option. Here's an example:
While we've discussed a handful of xz command line options in this tutorial, there are plenty more. Once you are done understanding and practicing these, head to the tool's man page to learn more about it.