XZ Compression in Linux Made Easy

xz compression in Linux is one of the easiest ways to shrink large files without giving up much on speed. It's a command line tool that uses the LZMA2 algorithm, trading a bit of extra CPU time for noticeably smaller files than gzip, which is why you will see it used for kernel tarballs, distro packages, and long term backup archives.

xz Command Syntax






LinuxTeck
xz [options] [file names]

I broke a cron backup job years ago because I assumed xz would leave my original log files in place after compressing them, the same way zip does. It doesn't, it deletes the source by default unless you tell it not to. Once you get past that one habit, xz becomes a pretty easy tool to add to your daily workflow, and if you are still getting comfortable at the terminal in general our basic Linux commands guide is a good companion piece. By the end of this article you will know exactly which flag to reach for depending on whether you want speed, size, or safety.

Examples


How to Install xz in Linux

Getting set up for xz compression in Linux takes one line, no third party repo needed. It usually gets installed alongside tar as part of a broader compression and archiving toolkit anyway.






LinuxTeck
# Ubuntu / Debian
linuxteck@ubuntu:~$ sudo apt install xz-utils

# RHEL / Rocky Linux / Fedora
linuxteck@rocky:~$ sudo dnf install xz

# Arch Linux
linuxteck@arch:~$ sudo pacman -S xz

Sample Output
xz (XZ Utils) 5.6.2
liblzma 5.6.2

1. Compress a File with xz

This is the simplest form of xz compression in Linux, run with no flags at all. It uses the default preset level of 6, which is a solid middle ground between speed and file size for most everyday files.






LinuxTeck
linuxteck@ubuntu:~$ xz backup.log
Sample Output
linuxteck@ubuntu:~$ ls
backup.log.xz

Notice backup.log is gone from the listing. xz replaced it with backup.log.xz, which is expected behavior and not a bug.

2. Keep the Original File While Compressing

Add the -k flag when you need both the compressed copy and the original file to stick around, which is common when you are archiving something you still need to read right away.






LinuxTeck
linuxteck@ubuntu:~$ xz -k config.json
Sample Output
linuxteck@ubuntu:~$ ls
config.json config.json.xz

3. Decompress an .xz File

Use -d, or the unxz command if you prefer typing it out, to restore a compressed file back to its original state.






LinuxTeck
linuxteck@ubuntu:~$ xz -d backup.log.xz
Sample Output
linuxteck@ubuntu:~$ ls
backup.log

4. Check Compression Ratio Without Extracting

The -l flag lists details about an xz file, including original size, compressed size, and the ratio between them, without touching the file itself. It's a faster habit than reaching for du on the extracted contents just to see how much space you saved.






LinuxTeck
linuxteck@ubuntu:~$ xz -l config.json.xz
Sample Output
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 412 B 1,240 B 0.332 CRC64 config.json.xz

5. Set the Compression Level for xz Compression in Linux

Choosing the right preset level is where xz compression in Linux really shows its flexibility. Levels run 0 through 9, lower numbers finish fast but leave a bigger file, higher numbers squeeze harder and take longer. This is worth learning if you are dealing with large disk space constraints on a server.






LinuxTeck
linuxteck@ubuntu:~$ cp access.log access_fast.log
linuxteck@ubuntu:~$ cp access.log access_best.log
linuxteck@ubuntu:~$ xz -0 access_fast.log
linuxteck@ubuntu:~$ xz -9 access_best.log
Sample Output
linuxteck@ubuntu:~$ xz -l access_fast.log.xz access_best.log.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 11.4 MiB 41.2 MiB 0.277 CRC64 access_fast.log.xz
1 1 8.9 MiB 41.2 MiB 0.216 CRC64 access_best.log.xz

Production Tip:

On a nightly backup job where the box is idle overnight, level 9 with -e (covered next) usually makes sense. On something that runs during business hours, stick closer to the default or you will spike CPU on a machine people are actively using.

6. Squeeze Out Extra Ratio with Extreme Mode

Adding -e to any preset level enables extreme mode, which spends more CPU time hunting for a smaller file at the same level. It's most useful paired with -9 for archives you are keeping long term.






LinuxTeck
linuxteck@rocky:~$ xz -9e database-dump.sql
Sample Output
linuxteck@rocky:~$ ls
database-dump.sql.xz

7. Speed Up Compression with Multiple Threads

By default xz compresses on a single core, which is painfully slow on a big file. The -T flag turns on multithreading, and -T 0 tells it to use every core available.






LinuxTeck
linuxteck@ubuntu:~$ xz -T 0 -k vmdisk.img
Sample Output
linuxteck@ubuntu:~$ time xz -T 0 -k vmdisk.img
real 1m42.311s
user 11m8.902s
sys 0m4.115s

The user time being far higher than real time is your confirmation that multiple cores actually kicked in.

8. Combine tar and xz to Compress a Directory

xz only works on a single file, it has no idea what to do with a folder. Pipe a directory through tar first with the -J flag and xz handles the compression stage automatically. This pairs well with any backup strategy you already run.






LinuxTeck
linuxteck@ubuntu:~$ tar -cJf website-backup.tar.xz /var/www/html
Sample Output
tar: Removing leading '/' from member names
linuxteck@ubuntu:~$ ls -lh website-backup.tar.xz
-rw-r--r-- 1 linuxteck linuxteck 214M Aug 25 09:12 website-backup.tar.xz

9. Extract a .tar.xz Archive

Reverse the process with a lowercase x for extract instead of create, keeping the same uppercase J for the xz format. This is the same pattern you would follow for any backup and restore routine on a Linux server.






LinuxTeck
linuxteck@ubuntu:~$ tar -xJf website-backup.tar.xz -C /restore
Sample Output
linuxteck@ubuntu:~$ ls /restore
html

10. Decompress to stdout Without Writing a File

Use xzcat, or xz -dc, when you want to read the contents of a compressed log straight in the terminal without leaving a decompressed copy sitting on disk.






LinuxTeck
linuxteck@ubuntu:~$ xzcat access.log.xz | grep "500"
Sample Output
10.0.0.14 - - [25/Aug/2026:03:12:09] "GET /checkout HTTP/1.1" 500 312
10.0.0.22 - - [25/Aug/2026:03:14:51] "POST /api/order HTTP/1.1" 500 118

11. Test the Integrity of an Archive Before Trusting It

Before you delete a source file or ship an archive off to cloud storage, run -t to confirm nothing got corrupted during compression or transfer.






LinuxTeck
linuxteck@rocky:~$ xz -tv website-backup.tar.xz
Sample Output
website-backup.tar.xz (1/1)
100 % 214.3 MiB / 812.6 MiB = 0.264

website-backup.tar.xz: OK

12. Trying to xz a Directory Directly

New users often try to point xz straight at a folder the same way they would with zip, and it refuses every time. If you are not sure which folders are eating up the most space before you archive them, the find command is worth pairing with xz for that kind of cleanup.






LinuxTeck
linuxteck@ubuntu:~$ xz /var/www/html
Sample Output
xz: /var/www/html: Is a directory, skipping

Common Mistake:

xz has no concept of a directory structure, it only ever compresses a single stream of bytes. You always need tar to bundle the folder first, then let xz compress that single tarball, as shown in example 8.


Conclusion

Once you get comfortable pairing xz with tar and know which compression level fits the job, it becomes one of those tools you reach for without thinking about it. If you are setting up recurring backups around this, our guide on writing an automatic Linux backup script is a natural next step, and if you are hosting the server these backups run on, a solid host like the one covered in our Kinsta review can save you a lot of headaches down the line. Which of these xz examples do you find most useful? Share your experience in the comments below.

LinuxTeck - A Complete Linux Learning Blog
From your first terminal command to advanced sysadmin skills, every guide here is written in plain English with real examples you can run right now.

Support My Work

Thank you for reading and for being part of this journey. If this article saved you time, consider buying me a coffee. Every contribution helps me keep producing in-depth, practical Linux content for readers like you.

Thank you for your endless support

About Aneeshya S

Aneeshya S is a Senior Linux Trainer and System Administrator with over 10 years of experience. She actively follows emerging technologies and industry trends. Outside the terminal, she enjoys music and travel.

View all posts by Aneeshya S →

Leave a Reply

Your email address will not be published.

L