xz Command
The xz command in Linux is a general-purpose command-line data compressor that uses the LZMA2 compression algorithm. It is primarily used for compressing or decompressing .xz files, offering very high compression ratios, often better than gzip or bzip2.
Syntax
Description
xz compresses or decompresses named files. If no files are given, it compresses or decompresses from standard input to standard output. It typically replaces the original file with the compressed or decompressed version.
Common uses include:
- Compressing large files for storage or transfer
- Decompressing software packages or archives
- Creating highly compressed backups
- Working with tar archives (e.g.,
.tar.xz)
Common Options
| Option | Description |
|---|---|
-d, --decompress, --uncompress |
Decompress. |
-z, --compress |
Compress. This is the default operation. |
-k, --keep |
Keep (don't delete) input files during compression or decompression. |
-f, --force |
Force operation. When compressing, overwrite existing output files. |
-v, --verbose |
Be verbose. Show the filename and percentage progress. |
-T NUM, --threads=NUM |
Use `NUM` threads for multi-core compression. |
-l, --list |
List information about .xz files. |
Examples
Compress a file
Compresses 'myfile.txt' into 'myfile.txt.xz' and deletes the original file.
Decompress a .xz file
Decompresses 'myfile.txt.xz' back to 'myfile.txt' and deletes the compressed file.
Compress and keep original file
Compresses 'anotherfile.log' into 'anotherfile.log.xz' but keeps the original file.
List contents of a .xz file
Lists information about the compressed file, such as compressed size, uncompressed size, and compression ratio.
Decompress to standard output
Decompresses 'myfile.txt.xz' and writes the output to 'decompressed_output.txt' without deleting the original .xz file.