How to Create and Extract XZ Compressed Tar Archives in Linux Terminal Using tar and xz

In modern Linux system administration and software distribution, the XZ compression format (typically packaged as .tar.xz or .txz) has largely superseded older compression standards like gzip (.tar.gz) and bzip2 (.tar.bz2). Leveraging the high-efficiency LZMA2 compression algorithm, XZ delivers dramatically smaller file footprints—often shaving an additional 15% to 30% off gzip sizes. For this reason, the official Linux kernel source trees, major Linux software distributions, and enterprise backup systems rely heavily on XZ archives.

Working with .tar.xz archives in the Linux terminal is straightforward using GNU tar and the underlying xz-utils package. This comprehensive guide walks you through creating, extracting, inspecting, and optimising XZ archives, including how to enable multi-threaded compression to saturate all available CPU cores.

Verifying and Installing xz-utils

Modern Linux distributions come with native XZ support pre-installed. However, if your minimal installation or container environment lacks the underlying compression binaries, install xz-utils via your package manager:

Ubuntu, Debian, and Linux Mint

sudo apt update
sudo apt install xz-utils tar

Fedora, RHEL, and CentOS

sudo dnf install xz tar

Arch Linux and Manjaro

sudo pacman -S xz tar

How to Create an XZ Compressed Tar Archive

To bundle multiple files or an entire folder structure into an XZ-compressed tarball, invoke tar with the -J flag (which instructs tar to pipe the stream through the XZ compressor):

tar -cJf archive.tar.xz /path/to/directory

Flag Breakdown:

  • -c: Instructs tar to create a new archive.
  • -J: Compresses the archive using the XZ algorithm. (Capital ‘J’ represents XZ, whereas lowercase ‘z’ is gzip and ‘j’ is bzip2).
  • -f: Specifies the output file name.
  • -v: (Optional) Enables verbose mode, printing the path of each processed file to the terminal screen.

To include verbose progress while packaging a project directory, run:

tar -cvJf backup.tar.xz /var/www/html

How to Extract an XZ Compressed Tar Archive

Extracting a .tar.xz file can be accomplished using either explicit XZ flags or modern GNU tar’s automatic format detection:

Standard Extraction into Current Directory:

tar -xJf archive.tar.xz

Modern versions of GNU tar can automatically recognise XZ compression headers from the magic byte signature, allowing you to omit -J:

tar -xf archive.tar.xz

Extracting to a Specific Directory:

To unpack the contents of an archive into an alternate destination folder rather than cluttering your current working directory, append the -C flag:

tar -xf archive.tar.xz -C /opt/destination/

How to Inspect and View Archive Contents Without Extracting

Before unpacking an unfamiliar archive onto your filesystem, you should always audit its contents to prevent tarbombing (unzipping thousands of uncontained files into the current folder):

tar -tvf archive.tar.xz

This displays a detailed table listing file permissions, ownership, file size in bytes, modification timestamps, and directory structures without writing any data to disk.

Speeding Up Compression with Multi-Threading (XZ_OPT)

Because the LZMA2 algorithm performs intensive mathematical dictionary calculations to achieve high compression ratios, single-threaded XZ compression can be relatively slow on large datasets. You can dramatically accelerate compression by instructing XZ to utilise all available CPU cores via the XZ_OPT environment variable:

# Use all available CPU cores (-T0)
XZ_OPT="-T0" tar -cvJf database_backup.tar.xz /data/sql/

# Use 4 specific processor threads
XZ_OPT="-T4" tar -cvJf files.tar.xz /my_files/

The -T0 parameter instructs the XZ engine to detect the number of logical CPU cores dynamically and parallelise the compression workload, slashing compression times by up to 75% on multi-core systems.

Adjusting Compression Levels: Fast vs Maximum Ratio

XZ supports compression presets ranging from 0 (fastest, lowest compression) to 9 (slowest, maximum compression). The default level is 6, which provides the best balance between memory overhead and file size reduction.

  • For Fast Compression: Use level 1 when speed is prioritised:
    XZ_OPT="-1 -T0" tar -cJf quick_archive.tar.xz ./project
  • For Maximum Space Savings: Use level 9 for cold storage and archival distributions:
    XZ_OPT="-9 -T0" tar -cJf ultra_compressed.tar.xz ./project

Working with Standalone Files Using xz and unxz

If you only need to compress an individual file (such as a database SQL dump, virtual machine disk image, or large log file) without archiving multiple folders into a tar container, use xz directly:

Compress a Single File:

# Compresses filename.log and replaces it with filename.log.xz
xz filename.log

# Keep original source file intact using -k
xz -k filename.log

Decompress a Single File:

# Decompresses filename.log.xz back to filename.log
unxz filename.log.xz

# Or using xz with the -d flag
xz -d filename.log.xz

Mastering XZ tar commands gives you the tools to create compact backups, transfer large datasets across bandwidth-constrained networks efficiently, and handle modern Linux distribution packages with ease.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.