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.

More in This Category
Linux less Command: Browse and Search Long Text Files

Linux less Command: Browse and Search Long Text Files

Learn how to browse long files with Linux less, move forward and backward, search text, follow growing logs, preserve colors, and exit the pager.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux tee Command: Display and Save Pipeline Output

Linux tee Command: Display and Save Pipeline Output

Learn how to display and Save Pipeline Output with the Linux tee command, including practical examples, key options, and important precautions.

Linux passwd Command: Change a User Password

Linux passwd Command: Change a User Password

Learn how to change a User Password with the Linux passwd command, including practical examples, key options, and important precautions.

Linux free Command: Check Memory and Swap Usage

Linux free Command: Check Memory and Swap Usage

Learn how to use the Linux free command to inspect available memory, used memory, cache, and swap usage, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Learn how to inspect and Configure Network Interfaces and Routes with the Linux ip command, including practical examples, key options, and important precautions.

Linux dpkg Command: Inspect and Install Local DEB Packages

Linux dpkg Command: Inspect and Install Local DEB Packages

Learn how to inspect and Install Local DEB Packages with the Linux dpkg command, including practical examples, key options, and important precautions.

Linux uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname command, including practical examples, key options, and important precautions.