xz Command in Linux: Compress and Decompress .xz Files

Deepak Prasad
Tested on Ubuntu 25.04 (Plucky Puffin)
Package xz-utils
Applies to Ubuntu, Debian, RHEL, Fedora
Privilege sudo or root
Man page xz(1)
Scope xz compresses single files into the .xz format using LZMA2. It replaces the original by default, supports integrity tests, preset levels, and pairs with tar for .tar.xz archives.
Related guides bzip2
cat
Linux commands

xz — quick reference

Compress and decompress

xz works on one file per invocation. Directories need tar first.

When to use Command
Compress a file (replaces input with .xz) xz file.txt
Force compression mode explicitly xz -z file.txt
Decompress .xz back to the original name xz -d file.txt.xz
Same as decompress — unxz helper unxz file.txt.xz
Decompress to stdout without removing .xz xzcat file.txt.xz

Safety and integrity

When to use Command
Keep the original file after compression xz -k file.txt
Overwrite existing output xz -f file.txt
Test archive integrity (no extract) xz -t file.txt.xz
Verbose integrity test with ratio xz -tv file.txt.xz

Levels and inspection

When to use Command
Fastest preset (lower ratio) xz -1 file.txt
Default preset (6) xz file.txt
Maximum preset (slower, smaller) xz -9 file.txt
Extra CPU for marginally better ratio xz -e file.txt
List compressed file metadata xz -l file.txt.xz

Multi-file archives

When to use Command
Create .tar.xz from several files tar -cJf archive.tar.xz file1 file2
List contents without extracting tar -tJf archive.tar.xz

Quiet, help, and version

When to use Command
Suppress warnings xz -q file.txt
Brief help xz -h
Version xz --version

xz — command syntax

Synopsis from xz --help on Ubuntu 25.04 (XZ Utils 5.6.4):

text
xz [OPTION]... [FILE]...

With no FILE, or when FILE is -, xz reads standard input and writes to stdout (it refuses compressed output to a terminal). Default action is compress; -d selects decompress.


xz — command examples

Essential Compress and decompress one file

Default compression removes the plaintext and leaves filename.xz. Decompress with -d.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo "This is a test file for xz compression." > /tmp/xz-demo/sample.txt
xz -v /tmp/xz-demo/sample.txt
ls -la /tmp/xz-demo/
xz -d /tmp/xz-demo/sample.txt.xz
ls /tmp/xz-demo/sample.txt
rm -rf /tmp/xz-demo

Sample output:

output
/tmp/xz-demo/sample.txt: 120 B / 56 B = 2.143
sample.txt.xz
sample.txt

The verbose line shows uncompressed size, compressed size, and ratio.

Essential List metadata and test integrity

-l reads the index without decompressing fully. -t verifies checksums — silent on success.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo "integrity check" > /tmp/xz-demo/sample.txt
xz /tmp/xz-demo/sample.txt
xz -l /tmp/xz-demo/sample.txt.xz
xz -t /tmp/xz-demo/sample.txt.xz && echo "integrity OK"
rm -rf /tmp/xz-demo

Sample output:

output
Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       1        120 B         56 B  2.143  CRC64   sample.txt.xz
integrity OK

Corrupt files make -t exit non-zero with an error message.

Common Keep original (-k) and force overwrite (-f)

Use -k when you need both plaintext and .xz. Use -f when the target .xz already exists.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo "keep me" > /tmp/xz-demo/keep.txt
xz -k /tmp/xz-demo/keep.txt
ls /tmp/xz-demo/
xz -f /tmp/xz-demo/keep.txt
ls /tmp/xz-demo/
rm -rf /tmp/xz-demo

Sample output:

output
keep.txt  keep.txt.xz
keep.txt.xz

After the second xz without -k, only the compressed file remains.

Common Stdout and xzcat

-c / xzcat prints decompressed bytes to stdout and leaves the .xz on disk.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo "stream me" > /tmp/xz-demo/keep.txt
xz /tmp/xz-demo/keep.txt
xzcat /tmp/xz-demo/keep.txt.xz
ls /tmp/xz-demo/keep.txt.xz
rm -rf /tmp/xz-demo

Sample output:

output
stream me
keep.txt.xz

Pipe-friendly for previewing logs without extracting to disk.

Common Compression presets -1 and -9

Presets trade CPU time for ratio. Default is -6. Small text files may show identical sizes at -1 and -9.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo "repeated data line for compression ratio test" > /tmp/xz-demo/a.txt
cp /tmp/xz-demo/a.txt /tmp/xz-demo/b.txt
xz -1 /tmp/xz-demo/a.txt
xz -9 /tmp/xz-demo/b.txt
xz -l /tmp/xz-demo/a.txt.xz /tmp/xz-demo/b.txt.xz
rm -rf /tmp/xz-demo

Sample output:

output
Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       1        112 B         46 B  2.435  CRC64   a.txt.xz
    1       1        112 B         46 B  2.435  CRC64   b.txt.xz

On large inputs, -9 usually wins on ratio; benchmark on your data before batching.

Advanced tar.xz archive for multiple files

xz alone does not pack directories. tar -cJ (or -I xz) builds a .tar.xz bundle.

bash
rm -rf /tmp/xz-demo && mkdir -p /tmp/xz-demo
echo one > /tmp/xz-demo/f1.txt
echo two > /tmp/xz-demo/f2.txt
tar -cJf /tmp/xz-demo/archive.tar.xz -C /tmp/xz-demo f1.txt f2.txt
tar -tJf /tmp/xz-demo/archive.tar.xz
rm -rf /tmp/xz-demo

Sample output:

output
f1.txt
f2.txt

Extract later with tar -xJf archive.tar.xz.

Advanced Detect invalid .xz data

Random bytes or truncated downloads fail integrity tests — the error is explicit.

bash
echo "not xz data" > /tmp/fake.txt.xz
xz -t /tmp/fake.txt.xz 2>&1 || true
rm -f /tmp/fake.txt.xz

Sample output:

output
xz: /tmp/fake.txt.xz: File format not recognized

Run -t after downloads before deleting originals.


xz — when to use / when not

Use xz when Use something else when
You need strong compression for single large files (kernels, tarballs) You need fastest compression on small logs → gzip
You distribute .tar.xz release tarballs You need in-place disk compression → filesystem tools
You verify downloads with xz -t You need to compress many files without tar → zip or tar first
CPU time is acceptable for smaller artifacts You need parallel compression at archive level → lbzip2 or pixz
Ratio matters more than speed You need only light archival on ancient hardware → gzip

xz vs gzip

xz gzip
Algorithm LZMA2 DEFLATE
Typical ratio Better on large files Faster, slightly larger
Default preset -6 -6
Common extension .xz .gz
tar flag tar -cJ / -I xz tar -cz
Decompress speed Often slower Usually faster

Pick xz for release tarballs; gzip for quick log rotation.


xz — interview corner

What is the xz command used for?

xz compresses one file into the .xz container format using LZMA2. By default it deletes the input after success and leaves filename.xz. Decompress with xz -d, unxz, or stream with xzcat.

For folders, combine with tar to produce .tar.xz.

A strong answer is:

"xz compresses single files to .xz with strong LZMA2 ratio. Directories go through tar -cJ. Default removes the input; -k keeps it."

When do you choose xz over gzip?

Choose xz when download size or archive storage dominates and CPU time is acceptable — common for kernel sources and release tarballs. Choose gzip when compressing many small logs quickly or when decompress speed matters more.

A strong answer is:

"xz for smaller release artifacts; gzip for speed and log rotation. I benchmark on representative files before standardizing."

How do you verify an .xz file without extracting?

Run xz -t file.xz. Add -v to see ratio while testing. Exit code zero means checksums passed.

bash
xz -tv release.tar.xz

A strong answer is:

"xz -t checks integrity without full extract; -v shows progress. I run it on downloaded tarballs before deleting sources."

What do xz compression levels mean?

Levels -0 through -9 trade speed for ratio; default is -6. -e spends more CPU for incremental gains. Levels -7-9 raise decompressor memory — check man page before using on small embedded systems.

A strong answer is:

"Presets 0–9, default 6; -9 is slowest/smallest. -e tries harder. I use -1 for quick tests and -6/-9 for releases."

How do you create a tar.xz archive?

Use tar with -J (lowercase j is bzip2; uppercase J is xz):

bash
tar -cJf backup.tar.xz /path/to/data

List with tar -tJf; extract with tar -xJf.

A strong answer is:

"tar -cJf archive.tar.xz files — uppercase J for xz. Extract with tar -xJf. xz alone only handles one file."


Troubleshooting

Symptom Likely cause Fix
File format not recognized Truncated download or wrong extension Re-download; run xz -t
File exists on compress file.xz already present Remove old file or use -f
Input vanished unexpectedly Default replaces input Use -k to keep original
Cannot compress to terminal Compressed stdout to TTY Redirect to a file or use -c > out.xz
Memory usage errors on -9 Decompressor RAM limit on small system Use lower preset or split archive
Cannot compress directory xz is single-file only tar -cJf archive.tar.xz dir/
xz: command not found xz-utils not installed sudo apt install xz-utils

References

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.

  • Linux
  • HTML5
  • JavaScript
  • Web Design
  • Front-end Web Development