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
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
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
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
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
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
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
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_best.log
linuxteck@ubuntu:~$ xz -0 access_fast.log
linuxteck@ubuntu:~$ xz -9 access_best.log
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
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
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:~$ 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
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
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
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
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.
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.
