How to Extract tar.xz Files in Linux

By 

Updated on

8 min read

Extract tar.xz files in Linux

When you download source code, Linux packages, or backup archives, you may find files that end in .tar.xz or .txz. These are tar archives compressed with xz, and you can extract them with one command:

Terminal
tar -xf archive.tar.xz

The tar command creates and extracts tar archives. It can also read common compression formats, including gzip, bzip2, and xz. By convention, a tar archive compressed with xz ends with either .tar.xz or .txz.

This guide explains how to extract .tar.xz and .txz files, list archive contents before extracting, unpack files to another directory, and fix common extraction errors.

Quick Reference

For a printable quick reference, see the tar cheatsheet .

TaskCommand
Extract tar.xztar -xf archive.tar.xz
Extract .txztar -xf archive.txz
Extract with verbose outputtar -xvf archive.tar.xz
Extract to another directorytar -xf archive.tar.xz -C /path/to/dir
List contentstar -tf archive.tar.xz
List contents with detailstar -tvf archive.tar.xz
Extract specific filestar -xf archive.tar.xz file1 file2
Extract matching filestar -xf archive.tar.xz --wildcards '*.png'
Extract from stdinwget -O - URL | tar -xJ
Preserve permissionstar -xpf archive.tar.xz
Skip existing filestar -xf archive.tar.xz --skip-old-files

Extracting tar.xz Files

The tar utility is pre-installed on most Linux distributions and macOS. For a regular .tar.xz file stored on disk, use tar -xf followed by the archive name:

Terminal
tar -xf archive.tar.xz

The -x option tells tar to extract files, and -f tells it to read from the archive file that follows. Modern tar versions auto-detect the xz compression, so you do not need to add the -J option when extracting a local file.

The same command also works for the shorter .txz extension:

Terminal
tar -xf archive.txz

The archive contents are extracted into the current working directory . If the archive contains a top-level directory, tar recreates that directory and places the files inside it.

To print each file name as it is extracted, add the -v option:

Terminal
tar -xvf archive.tar.xz

Verbose output is useful when you want to confirm that tar is working or when you need to see the paths stored inside the archive.

The tar -xf pattern also works with other tar compression formats, such as .tar.gz and .tar.bz2 , when your tar version supports automatic compression detection.

Extracting to Another Directory

Use the --directory (-C) option to extract the archive into a specific directory. The target directory must already exist.

For example, to extract archive.tar.xz into /home/linuxize/files, run:

Terminal
tar -xf archive.tar.xz -C /home/linuxize/files

If the directory does not exist, create it first:

Terminal
mkdir -p /home/linuxize/files
tar -xf archive.tar.xz -C /home/linuxize/files

This keeps the extracted files away from your current directory and makes cleanup easier if the archive contains many paths.

Listing tar.xz File Contents

Before extracting an archive from an untrusted or unfamiliar source, list its contents. This shows where files will be written.

To list the contents of a .tar.xz file, use the --list (-t) option:

Terminal
tar -tf archive.tar.xz

The output shows the paths stored in the archive:

output
file1
file2
file3

For a detailed listing with permissions, owners, sizes, and timestamps, add -v:

Terminal
tar -tvf archive.tar.xz

The output will look similar to this:

output
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file1
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file2
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file3

Use this list when you need the exact path for extracting one file or directory.

Extracting Specific Files from a tar.xz File

To extract specific files from a .tar.xz archive, place their archive paths after the archive name:

Terminal
tar -xf archive.tar.xz file1 file2

The file names must match the paths shown by tar -tf. If the archive stores files under a directory such as project/, include that directory in the path:

Terminal
tar -xf archive.tar.xz project/file1 project/file2

You can extract directories the same way:

Terminal
tar -xf archive.tar.xz project/docs

If you try to extract a path that does not exist in the archive, tar prints an error like this:

Terminal
tar -xf archive.tar.xz README
output
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

The message means the path you provided does not match the path stored in the archive. Run tar -tf archive.tar.xz and copy the exact path from the listing.

You can also extract files by wildcard pattern. Use the --wildcards option and quote the pattern so your shell does not expand it before tar sees it.

For example, to extract files whose names end in .png, run:

Terminal
tar -xf archive.tar.xz --wildcards '*.png'

Extracting tar.xz Files from stdin

When reading a .tar.xz archive from standard input, usually through a pipe, specify the xz decompression option. The -J option tells tar that the incoming stream is compressed with xz.

The following example downloads a Linux kernel archive with wget and extracts it from the stream:

Terminal
wget -c https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.12.68.tar.xz -O - | sudo tar -xJ

When extracting from stdin, there is no file extension for tar to inspect. If you leave out -J, tar may print this message:

output
tar: Archive is compressed. Use -J option
tar: Error is not recoverable: exiting now

Add -J and run the command again.

To extract the stream into a specific directory, combine -J with -C:

Terminal
wget -O - https://example.com/archive.tar.xz | tar -xJ -C /tmp/files

Extracting and Preserving Permissions

By default, tar preserves file modes when extracting. If you are restoring system files as root and want to preserve the original permissions more explicitly, use the -p option:

Terminal
sudo tar -xpf archive.tar.xz

This keeps the permissions stored in the archive. Ownership handling depends on the user running the command and the metadata stored in the archive.

For archives from unknown sources, inspect the contents before extracting as root:

Terminal
tar -tvf archive.tar.xz

This helps you spot unexpected paths or ownership before writing files to the system.

Handling Existing Files

When extracting, tar overwrites existing files by default. To keep existing files and skip archive files with the same path, use --skip-old-files:

Terminal
tar -xf archive.tar.xz --skip-old-files

To keep newer files on disk and overwrite only older files, use --keep-newer-files:

Terminal
tar -xf archive.tar.xz --keep-newer-files

If you want tar to fail instead of overwriting an existing file, use --keep-old-files:

Terminal
tar -xf archive.tar.xz --keep-old-files

This is useful when you are extracting into a directory that already contains important files.

Extracting tar.xz Files without the Command Line

Most Linux desktop file managers can extract .tar.xz files. Right-click the archive and choose the extract option from the context menu.

On Windows, tools such as 7-Zip can open and extract .tar.xz files. A .tar.xz archive has two layers, so the tool may first decompress the .xz layer and then show the .tar archive inside it.

Troubleshooting

tar: Archive is compressed. Use -J option
This usually happens when you read a .tar.xz archive from stdin without telling tar which decompressor to use. Add -J:

Terminal
cat archive.tar.xz | tar -xJ

For local files, prefer tar -xf archive.tar.xz, because tar can usually detect the compression from the file itself.

tar: README: Not found in archive
The file path you typed does not match the path inside the archive. List the archive contents first:

Terminal
tar -tf archive.tar.xz

Then copy the exact file path from the output and use it in the extract command.

tar: archive.tar.xz: Cannot open: No such file or directory
The archive name or path is wrong, or you are running the command from a different directory. Check the file name:

Terminal
ls

If the archive is in another directory, provide the full path:

Terminal
tar -xf /path/to/archive.tar.xz

xz support is missing
Most current Linux systems include xz support, but minimal installations may not. On Ubuntu, Debian, and derivatives, install the xz tools with:

Terminal
sudo apt install xz-utils

On Fedora, RHEL, and derivatives, install the xz package with:

Terminal
sudo dnf install xz

After installing the package, run the tar -xf archive.tar.xz command again.

You have an .xz file, not a .tar.xz file
A .tar.xz file is a tar archive compressed with xz. A plain .xz file is usually one compressed file, not an archive. To decompress a plain .xz file, use:

Terminal
unxz file.xz

If you need to keep the original .xz file, use:

Terminal
xz -dk file.xz

FAQ

What is the difference between tar.xz and tar.gz?
Both are tar archives compressed with different algorithms. xz uses LZMA2 and usually produces smaller files, but compression is slower. gzip is faster, but the resulting archive is often larger. For local files, the extraction command is the same: tar -xf archive-name.

Do I need to install xz to extract tar.xz files?
On most Linux distributions, xz is pre-installed. If tar reports that xz support is missing, install it with sudo apt install xz-utils on Debian/Ubuntu or sudo dnf install xz on Fedora/RHEL.

What does the -J flag do?
The -J flag tells tar to use xz compression or decompression. You usually need it when reading from stdin or when creating a .tar.xz archive with tar -cJf. When extracting a file from disk, tar -xf archive.tar.xz is normally enough.

Is tar.xz the same as xz?
No. A .tar.xz file is a tar archive compressed with xz, so it can contain many files and directories. A .xz file is usually a single compressed file. Use tar -xf archive.tar.xz for .tar.xz files and unxz file.xz for plain .xz files.

Can I extract a tar.xz file on macOS?
Yes. The tar command on macOS supports xz extraction. Use the same tar -xf archive.tar.xz command.

How do I create a tar.xz archive?
Use tar -cJf archive.tar.xz file1 file2 dir1. The -c flag creates the archive, and -J compresses it with xz. For more details, see our guide on creating tar archives .

Conclusion

The tar -xf archive.tar.xz command is the simplest way to extract .tar.xz and .txz archives from disk. For broader archive workflows, see the full tar command guide and the related guides for tar.gz and tar.bz2 files.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page