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 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 / File Types and Metadata: ls, file, stat, and readlink

Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

Learn what ls, file, stat, and readlink each reveal about a Linux file, including type, size, timestamps, and symbolic-link targets.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Learn how to remove or count adjacent duplicate lines with the Linux uniq command, including practical examples, key options, and important precautions.

Linux pacman Command: Manage Packages on Arch Linux

Linux pacman Command: Manage Packages on Arch Linux

Learn how to manage Packages on Arch Linux with the Linux pacman command, including practical examples, key options, and important precautions.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux command Command: Run Commands While Bypassing Aliases and Functions

Linux command Command: Run Commands While Bypassing Aliases and Functions

Learn how to use the Linux command command to check command availability and bypass shell aliases or functions, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux top Command: Monitor Processes in Real Time

Linux top Command: Monitor Processes in Real Time

Learn how to monitor Processes in Real Time with the Linux top command, including practical examples, key options, and important precautions.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux sort Command: Sort Text Lines

Linux sort Command: Sort Text Lines

Learn how to sort text lines with the Linux sort command, including practical examples, key options, and important precautions.