Compression and decompression of files in Linux can be achieved using various command-line tools. In this tutorial, we’ll focus on the xz command, a powerful tool for data compression. All examples and instructions are demonstrated on an Ubuntu 18.04 LTS machine.
Linux xz Command
The xz command is a versatile compression tool with syntax similar to gzip and bzip2. Its usage can be summarized as follows:
xz [option...] [file...]
According to the manual:
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 to write compressed data to standard output if it is a terminal and will refuse to read compressed data from standard input if it is a terminal.
Examples of xz Command Usage
Q1. How to Use the xz Command?
Basic usage involves specifying the file to compress:
xz file.txt
This command compresses file.txt
into file.txt.xz
, replacing the original file.
Q2. Retaining the Original File
To retain the original file during compression, use the -k
option:
xz -k file.txt
Now, both file.txt
and file.txt.xz
will exist in the directory.
Q3. Compressing Multiple Files
Compress multiple files simultaneously by listing them as arguments:
xz file1.txt file2.txt
This results in file1.txt.xz
and file2.txt.xz
.
Q4. Decompressing .xz Files
To decompress, use the -d
option:
xz -d file.txt.xz
This command decompresses the file, restoring file.txt
.
Q5. Displaying Information About Compressed Files
Use the -l
option to get details about a compressed file:
xz -l file.txt.xz
This displays information such as compressed size, uncompressed size, and compression ratio.
Q6. Specifying Different Compression/Decompression Formats
Choose a compression format with the -F
option:
-F format, --format=format Specify the file format to compress or decompress: auto Default format; chooses automatically xz Use the .xz format lzma Use the legacy .lzma format raw Compress or uncompress a raw stream (no headers)
Q7. Displaying a Progress Indicator
Show progress during compression with the -v
option:
Conclusion
We’ve covered essential xz command options in this guide. To explore more features, consult the tool’s man page.
Frequently Asked Questions
What is the xz format?
The xz format is a data compression format that offers high compression ratios, making it popular for packaging and distribution of software.
Can xz compress directories?
xz itself cannot compress directories directly. Use it in conjunction with tar
to first archive the directory into a single file, and then compress the archive.
Is the xz command compatible with Windows?
Yes, by using tools like 7-Zip, you can create and extract xz archives on Windows systems.
How can I install xz on my Linux distribution?
On most Linux distributions, xz is pre-installed. If not, it can be installed via the package manager, for example, sudo apt install xz-utils
on Ubuntu.