Compress files with xz

Squeeze logs, dumps and archives smaller than gzip can, with a single flag to keep the originals.

LinuxbackupFilescompressionLow risk
Run xz -k -9 big.log to compress while keeping the original; restore it later with xz -dk big.log.xz.

01Command

Command
$ xz -k -9 big.log

02What it does

xz (LZMA2) typically beats gzip and bzip2 by a wide margin on text, logs and database dumps, at the cost of slower compression. -k keeps the input file instead of replacing it, and presets -0 to -9 trade speed against size, with -9 getting the last few percent. xz -t verifies an archive without unpacking, and tar -cJf archive.tar.xz dir/ combines both tools.

03Risk

RiskLow risk
Requirementsxz package (xztutils); preinstalled on most distros. Decompression needs the xz utilities too.
!Warning: Level -9 can demand hundreds of MB of RAM per thread and is slow on big files — -6 (the default) is usually the smarter choice. A truncated or corrupt .xz file cannot be repaired in place, so keep checksums for long-term archives.

04Undo

Undo
$ xz -dk big.log.xz

05Sources

  • xz(1) — Arch Manual Pages
    Arch Linux · community · 2026-09-17
    Evidence: Same flags documented: --keep to not delete the input, --decompress, --test, and memory usage of the -9 preset.
  • xz(1) — Debian bookworm
    Debian Manpages · community · 2026-09-17
    Evidence: Documents -z/--compress, -d/--decompress, -k/--keep, -t/--test and the -0...-9 preset levels.

Last verified: 2026-09-19 · Category: Files & Folders

06Related solutions