Home > Archive/Compression > xz

xz: Compress/Decompress .xz and .lzma Files

The xz command is used to compress or decompress files using the LZMA2 compression algorithm. It offers higher compression ratios than gzip or bzip2, but may take longer to compress and decompress. It is particularly useful for compressing large files or archives (like tar files).

Overview

xz is a powerful compression tool based on the LZMA2 algorithm. It is primarily used when high compression ratios are needed, and it is especially effective when used in conjunction with archiving tools like tar.

Key Features

  • Uses LZMA2 compression algorithm
  • Provides high compression ratios
  • Deletes original files after compression/decompression by default
  • Integrates well with archiving tools like tar

Key Options

Compression/Decompression Control

Compression Levels

Output Control

Generated command:

Try combining the commands.

Description:

`xz` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Compress a File (Default)

xz myfile.txt

Compresses myfile.txt to create myfile.txt.xz and deletes the original file.

Decompress a File (Default)

xz -d myfile.txt.xz

Decompresses myfile.txt.xz to create myfile.txt and deletes the compressed file.

Compress While Keeping Original File

xz -k myfile.txt

Compresses myfile.txt to create myfile.txt.xz, but keeps the original myfile.txt.

Compress with Maximum Compression

xz -9k myfile.txt

Compresses myfile.txt with the highest compression level (-9) and keeps the original file.

Compress a Tar Archive

tar -cf - mydirectory/ | xz - > mydirectory.tar.xz

Bundles the mydirectory directory into a tar archive and then compresses it with xz to create mydirectory.tar.xz.

Decompress a Tar Archive

xz -dc mydirectory.tar.xz | tar -xf -

Decompresses mydirectory.tar.xz and then uses tar to extract the directory.

Tips & Notes

xz provides high compression ratios but consumes significant CPU resources and takes longer to process. This should be considered when applying it to large files.

Performance and Usage

  • Use xz when compression ratio is the priority; consider gzip for speed.
  • It is very useful for compressing entire directories when used with tar, resulting in files with the `.tar.xz` extension.
  • By default, original files are deleted after compression/decompression. It is recommended to use the `-k` option to preserve originals or redirect to standard output with the `-c` option to protect the original files.

Same category commands