Skip to content
shellmap

xzLZMA2 best-ratio compression, .tar.xz Linux kernel default, multi-threaded -T0, lost ground to zstd 2020s across all 5 shells

Equivalents in every shell

Bashunix
xz -T0 -9 file.txt

`xz FILE` → `FILE.xz` (and removes source by default — same trap as gzip/bzip2 — use `-k` to keep). `-1..-9` compression level (default `-6`). `-9e` "extreme" mode squeezes 1-2% more at much slower compress. `-T N` use N threads (`-T0` = all available cores). `-d` decompress. `unxz` is a symlink to `xz -d`. `xzcat`/`xzless`/`xzgrep` operate on .xz without extracting. Installed by default on every modern Linux + macOS Catalina+.

Zshunix
xz -T0 -9 file.txt
Fishunix
xz -T0 -9 file.txt
PowerShellwindows
& "$env:ProgramFiles\7-Zip\7z.exe" a -txz -mx=9 file.xz file.txt

pwsh has no native xz cmdlet — .NET 9 added `System.IO.Compression.LzmaCompressionMode` but no high-level streams. 7-Zip CLI is the practical answer (`-txz` for xz format). Alternative: install GNU xz for Windows (`winget install Xz.Xz` provides `xz.exe`).

cmd.exewindows
powershell -NoProfile -Command "& '$env:ProgramFiles\7-Zip\7z.exe' a -txz -mx=9 file.xz file.txt"

cmd has no xz. `7z a -txz file.xz file.txt` after `winget install 7zip.7zip`. Or `winget install Xz.Xz` for native `xz.exe`. WSL is the simplest path on Windows.

Worked examples

Compress with maximum compression + all CPU cores

Bash
xz -T0 -9e file.txt
Fish
xz -T0 -9e file.txt
PowerShell
& "$env:ProgramFiles\7-Zip\7z.exe" a -txz -mx=9 file.xz file.txt
cmd.exe
7z a -txz -mx=9 file.xz file.txt

Decompress (keep original)

Bash
xz -dk file.txt.xz
Fish
xz -dk file.txt.xz
PowerShell
& "$env:ProgramFiles\7-Zip\7z.exe" x file.txt.xz
cmd.exe
7z x file.txt.xz

Create a .tar.xz archive of a directory

Bash
tar -cJf archive.tar.xz dir/
Fish
tar -cJf archive.tar.xz dir/
PowerShell
tar -cJf archive.tar.xz dir
cmd.exe
tar -cJf archive.tar.xz dir

Gotchas

  • **xz is THE default for Linux source tarballs**. kernel.org: `linux-6.10.tar.xz` is ~130 MB; the same content as `.tar.gz` is ~220 MB; as `.tar.bz2` is ~190 MB. The ~40% size reduction over gzip is worth the slow-compress for distribution (compressed ONCE, decompressed by every user — and xz decompression is comparable to gzip speed). GNU autotools, GNU coreutils, Linux kernel, Python source tarballs — all `.tar.xz` by default since the early 2010s.
  • **`-9e` extreme mode is SLOW — be selective**. Default `-6` is the sweet spot for most data. `-9` adds ~1-2% compression at 2-3x compress time. `-e` adds another 1-2% at 1.5-2x more time. So `-9e` vs `-6` is ~5% smaller file at 5-10x compress time. For an archive that's downloaded thousands of times (kernel tarball, distro ISO), worth it. For a one-shot backup, not worth it. Decompress speed is the SAME regardless of compress level — paying compress time is amortized over many downloads.
  • **Multi-threaded compression by default since xz 5.2 (2014)** — `xz -T0` uses all available cores. Comparison: gzip is single-threaded (use `pigz` for parallel — separate binary); bzip2 single-threaded (`pbzip2` for parallel). xz built it in. Caveat: multi-threading SLIGHTLY hurts compression ratio (~1-2% worse) because the file is split into independent chunks. For maximum ratio with multi-thread: `xz -T0 --block-size=256MiB` (large blocks = less ratio loss). For maximum ratio period: `xz -T1` (single-threaded) — sacrifices speed.
  • **The XZ Utils 2024 backdoor (CVE-2024-3094)** — versions 5.6.0 and 5.6.1 contained a malicious upstream backdoor that injected via the test infrastructure into liblzma, ultimately allowing RCE via sshd on systemd-linked systems. Audit: `xz --version` — if 5.6.0 or 5.6.1, IMMEDIATELY downgrade to 5.4.x or upgrade to 5.6.2+ (the fixed version). The backdoor was the cleanest social-engineering supply-chain attack publicly documented; the takeaway is that compressed-binary blobs in test fixtures are now considered audit smell across all open-source projects.
  • **xz LOST GROUND to zstd in the 2020s** — same arc as bzip2 in the 2010s. zstd (Facebook, 2016+) hits a sweet spot: similar ratio to xz at zlib/gzip speeds, multi-threaded, supports streaming AND seekable formats. Modern defaults: Arch Linux packages (.pkg.tar.zst since 2019), Debian (apt offers zstd alternative since 2024), Fedora (.rpm uses zstd since 2018), OCI container images (zstd compression accepted since 2021). For NEW archives where compatibility allows: `zstd -19 file.txt` or `tar -I "zstd -19 -T0" -cf archive.tar.zst dir/`. xz remains for legacy/source-tarball context.

WSL & PowerShell Core notes

pwshpwsh has no native xz. Cross-platform pattern: shell to `xz` on Linux/macOS; on Windows, either `winget install Xz.Xz` for `xz.exe` or `7z -txz` via 7-Zip CLI. Pure-managed: NuGet `Joveler.Compression.XZ` wraps liblzma natively.
WSLWSL Linux `xz` works on the WSL filesystem and `/mnt/c/...`. For interop with Windows-side: `xz` output `.xz` files are byte-identical regardless of OS — Windows 7-Zip / WinRAR open them. For Windows-native xz: `winget install Xz.Xz` provides `xz.exe` in PATH.

Common tasks using xz

Related commands