How to extract xz files in Linux

.xz and .tar.xz archives are common for source releases, backups, and exported logs because they shrink data aggressively without changing the original file contents. Extracting them correctly restores either the original single file or the archived directory tree so the data can be reviewed, edited, or deployed normally.

A plain .xz file contains one compressed data stream, so xz or unxz recreates one output file when it is decompressed. A .tar.xz file wraps a tar archive inside that compressed stream, so tar can list the stored paths and unpack the whole tree in one step while preserving the archived directory structure.

Extraction needs enough free space for the uncompressed data and is safest in a dedicated target directory when the archive contains multiple files. By default xz removes the source archive after a successful decompression, so the steps below use --keep for plain .xz files and preview a .tar.xz archive before unpacking it into a restore directory.

Steps to extract xz files in Linux:

  1. Change to the directory that contains the archive and identify whether it is a plain .xz file or a .tar.xz archive.
    $ cd /tmp/archive-demo
    $ file project-notes.txt.xz project-backup.tar.xz
    project-notes.txt.xz:  XZ compressed data, checksum CRC64
    project-backup.tar.xz: XZ compressed data, checksum CRC64

    The file command confirms the compression format, but a .tar.xz archive still needs a tar listing step to show the stored file names. Files that end in .txz are the same kind of tar archive compressed with xz.

  2. List metadata for a plain .xz file before decompressing it.
    $ xz --list project-notes.txt.xz
    Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
        1       1         88 B         33 B  2.667  CRC64   project-notes.txt.xz

    xz --list prints size and integrity-check details without writing any output files.

  3. Decompress a plain .xz file while keeping the original archive.
    $ xz --decompress --keep project-notes.txt.xz
    $ ls -1 project-notes.txt*
    project-notes.txt
    project-notes.txt.xz

    Omit --keep when the compressed source file should be removed after a successful extraction. If the target filename already exists, xz stops instead of overwriting it unless --force is added deliberately.

  4. Preview the contents of a .tar.xz archive before extracting it.
    $ tar --list --file project-backup.tar.xz
    project-backup/
    project-backup/reports/
    project-backup/reports/report.txt
    project-backup/beta.log
    project-backup/alpha.txt

    Current GNU tar releases auto-detect compression when reading a normal archive file, so --xz is not required here. If the archive is being read from a pipe instead of a file, add --xz or -J to tell tar which decompressor to use.

  5. Create a target directory and extract the .tar.xz archive into it.
    $ mkdir -p restore
    $ tar --extract --verbose --file project-backup.tar.xz --directory=restore
    project-backup/
    project-backup/reports/
    project-backup/reports/report.txt
    project-backup/beta.log
    project-backup/alpha.txt

    Extracting into an existing directory can overwrite files that have the same archived paths and names. Listing the archive first and unpacking into an empty directory reduces that risk.

  6. Verify that the expected files and directories were restored.
    $ find restore -maxdepth 3 -print | sort
    restore
    restore/project-backup
    restore/project-backup/alpha.txt
    restore/project-backup/beta.log
    restore/project-backup/reports
    restore/project-backup/reports/report.txt

    A sorted listing is an easy final check after extraction, especially when the archive contains nested directories or when the restored tree will be compared against a source backup.