Linux xz Command: Compress a File in xz Format
xz compresses files into the .xz format. Since it is an individual file compression tool, you need to combine it with tar to preserve the directory structure.
What is the xz command?
By default, it changes file to file.xz and removes the original. -k is used to keep the original, -c to output to standard output, and -T to specify the number of compression threads. High compression levels can increase processing time and memory usage, so it is not necessary to always choose the maximum value.
Basic syntax
xz [option] [file...]
The installed implementation and options may vary depending on the distribution. Use man xz to check the description on your current system.
Examples
Compress while keeping the original
Leave the input file with -k.
xz -k database.sql
Save compression results under a separate name
Send compressed data to standard output. First, check whether the target file exists.
xz -c database.sql > database-copy.sql.xz
Check compressed file
Check compressed data errors without actually creating the extracted file.
xz -t database.sql.xz
Results of xz compression and test
xz compresses a single file into .xz format. By default, it replaces the original, so use -k to preserve the original.
xz -k note.txt xz -t note.txt.xz ls note.txt*
Example output:
note.txt note.txt.xz
xz -t checks the compressed data structure and usually gives no message when everything is normal. If a .xz file already exists, it may fail or ask for confirmation instead of overwriting. If your purpose is to combine multiple files into one, combine archiving and compression like tar -cJf.
Main Options and Format
| Options/Format | Description |
|---|---|
-k |
Keep the processed input file. |
-c |
Sends the result to standard output. |
-t |
Examine the compressed file. |
-0 ... -9 |
Select the compression level. |
-T number_of_threads |
Specify the number of threads for the compression task. |
Precautions when using
The default execution removes the original file. Higher settings like -9e may use more memory and time, and constraints of the restoration environment should also be considered. .xz is not a multi-file archive and does not provide file security or encryption.
Frequently Asked Questions
What is the difference between xz and tar.xz?
.xz is usually a single file compression stream, and .tar.xz is an archive created by combining multiple files with tar and then compressing them with xz.
Official Documentation
The exact behavior of options and implementation-specific differences can be checked in the official xz documentation.









