xz - Linux


Overview

The xz command is a file compression utility in Linux, utilizing the LZMA/LZMA2 compression algorithms. It is widely used for its high compression ratios, especially in software distribution and backup contexts. xz is often chosen for its efficient balance between compression speed and the level of compression achieved.

Syntax

The basic syntax of the xz command is as follows:

xz [options] [file ...]
  • [options]: These are the flags or arguments that determine the behavior of the command.
  • [file …]: One or more files to be compressed or decompressed.

Options/Flags

Here are some of the frequently used options in the xz utility:

  • -z, --compress: Force compression, which is the default when no file operation mode option is specified.
  • -d, --decompress, -uncompress: Decompress files.
  • -k, --keep: Keep (do not delete) input files.
  • -f, --force: Force overwrite of output file and (de)compress links.
  • -t, --test: Test compressed file integrity.
  • -c, --stdout, --to-stdout: Write to standard output and do not change input files.
  • -l, --list: List information about .xz files.
  • -q, --quiet: Suppress warnings; specify twice to suppress errors, too.
  • -v, --verbose: Provide verbose output; can be specified multiple times to increase verbosity.
  • -L, --license: Display the software license.
  • --threads=[number]: Use the specified number of threads for compression; --threads=0 uses as many threads as there are CPU cores.

Examples

  1. Compress a file:

    xz filename.txt
    

    This command compresses filename.txt into filename.txt.xz and deletes the original by default.

  2. Decompress a file:

    xz -d filename.txt.xz
    

    This restores the original filename.txt from the compressed filename.txt.xz.

  3. Compress multiple files:

    xz *.txt
    

    This compresses all .txt files in the current directory individually.

  4. Using verbosity and keeping the original file:

    xz -vk filename.txt
    

    Compress filename.txt to filename.txt.xz, keep the original file, and show verbose output.

  5. List details of a compressed file:

    xz -l filename.txt.xz
    

    Display detailed metadata about the compressed file.

Common Issues

  • File Already Exists: When attempting to compress a file into an existing .xz file, an error occurs. Use the -f flag to overwrite existing files.
  • Memory Usage: High compression levels can consume significant amounts of RAM. Adjust the compression level with -0 to -9 (-6 being the default).

Integration

Combine xz with other commands to handle large datasets or logs:

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

This creates a compressed archive from a directory, minimizing space usage.

xz -dkc file.xz | less

Decompress file.xz to stdout and view the content in less, without extracting it to disk.

  • gzip: Another compression tool, using a different algorithm, often faster but with lower compression ratio.
  • bzip2: Uses the Burrows-Wheeler block sorting text compression algorithm and Huffman coding, providing better compression ratios than gzip.
  • tar: Often used in conjunction with compression utilities like xz for creating archives of multiple files.

For more detailed information, consult the xz man page by entering man xz in your terminal or visiting the official XZ utils website.