A file ending in .tar.xz (or .txz) is a tar archive compressed with XZ, and a file ending in plain .xz is a single compressed file. On Linux and macOS, one command handles each:
tar -xf archive.tar.xzunxz file.xz
The first command unpacks every file and folder from the archive into the current directory. The second decompresses file.xz to file and deletes the .xz file. The rest of this guide covers extracting to another directory, listing and picking files before you extract, keeping the original .xz file, Windows and macOS, and fixes for common errors. All commands were tested with GNU tar 1.35 and XZ Utils 5.4.5 on Ubuntu 24.04, and they work the same on Ubuntu 26.04, Debian, Fedora, RHEL and Arch.
What Are .xz and .tar.xz Files?
XZ is a compression format that uses the LZMA2 algorithm. It usually makes smaller files than gzip and bzip2, which is why Linux distributions, the Linux kernel and many open-source projects ship their source code and packages as .tar.xz. The trade-off is speed: XZ compresses more slowly than gzip, but decompression is fast.
XZ compresses one file. It doesn’t bundle folders. That’s why you see two kinds of files:
| Extension | What it contains | Command to extract |
|---|---|---|
.tar.xz or .txz | A tar archive (many files and folders) compressed with XZ | tar -xf file.tar.xz |
.xz | One compressed file, for example a disk image or a log file | unxz file.xz or xz -d file.xz |
If you aren’t sure which one you have, ask the file command. Both report as XZ data, so the name is the main clue, and tar -tf (shown below) tells you for sure whether there is a tar archive inside.
file archive.tar.xz
archive.tar.xz: XZ compressed data, checksum CRC64
Install xz (If It’s Missing)
Most Linux systems have tar and XZ Utils installed already. If tar stops with xz: Cannot exec: No such file or directory, install the package:
sudo apt install xz-utils
That’s for Ubuntu and Debian. On other systems:
- Fedora, RHEL, Rocky Linux, AlmaLinux:
sudo dnf install xz - Arch Linux:
sudo pacman -S xz - openSUSE:
sudo zypper install xz - macOS (Homebrew):
brew install xz(you only need this for plain.xzfiles; see the macOS section)
Check the installed version with xz --version.
1. Extract a tar.xz File
Go to the folder that holds the archive and run tar -xf:
cd ~/Downloadstar -xf archive.tar.xz
-xextracts.-fnames the archive file. It must come last in the group of options, right before the file name.
You don’t need to tell tar that the archive is XZ-compressed. Modern GNU tar and bsdtar check the file and pick the right decompressor themselves (see the tip tar detects compression automatically). Many older guides use tar -xJf, where -J means XZ. It still works, and you need it only when tar reads from a pipe and can’t see the file:
tar -xJf archive.tar.xz
Add -v (verbose) to print each file name as it’s extracted:
tar -xvf archive.tar.xz
proj/
proj/sub/
proj/sub/nums.txt
proj/a.txt
tar keeps the original .tar.xz file. It doesn’t delete it.
2. Extract to a Different Directory
By default, tar writes into the current directory. Use -C to choose another one. The directory must already exist:
mkdir -p /opt/myapptar -xf archive.tar.xz -C /opt/myapp
Most source archives put everything in a top-level folder such as proj/. To drop that folder and put its contents straight into the target directory, add --strip-components=1:
tar -xf archive.tar.xz -C /opt/myapp --strip-components=1
Now proj/a.txt lands at /opt/myapp/a.txt instead of /opt/myapp/proj/a.txt.
3. List the Contents Without Extracting
It’s a good habit to look inside an archive from the internet before you unpack it, to see whether it has a top-level folder or will spill files into your current directory. Use -t (list) instead of -x:
tar -tf archive.tar.xz
proj/
proj/sub/
proj/sub/nums.txt
proj/a.txt
Add -v (tar -tvf) to see permissions, owners, sizes and dates, like ls -l. To see the compressed and uncompressed size and the compression ratio, use xz -l:
xz -l archive.tar.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 18.3 KiB 580.0 KiB 0.032 CRC64 archive.tar.xz
4. Extract Only Some Files
Put the paths you want after the archive name. Use the paths exactly as tar -tf prints them:
tar -xf archive.tar.xz proj/a.txt
To extract every file that matches a pattern, add --wildcards and quote the pattern so the shell doesn’t expand it:
tar -xf archive.tar.xz --wildcards '*.txt'
To print one file to the screen without writing anything to disk, use -O (capital letter O):
tar -xOf archive.tar.xz proj/a.txt
If some files already exist in the target directory, tar overwrites them. Add --skip-old-files to leave existing files alone, or --keep-old-files to stop with an error instead.
5. Decompress a Single .xz File
For a plain .xz file, use unxz. It’s the same as xz -d (decompress):
unxz file.xz
This creates file and deletes file.xz. To keep the compressed file, add -k:
unxz -k file.xz
Other useful options and tools:
unxz -f file.xzoverwritesfileif it already exists. Without-f, unxz stops withunxz: file: File exists.unxz *.xzdecompresses several files at once.xzcat file.xz(orxz -dc) prints the decompressed data to standard output and leaves the file untouched, for examplexzcat image.img.xz | sudo dd of=/dev/sdX bs=4M status=progressto write a disk image without unpacking it first.xzlessandxzgrepread and search a compressed text file, such as a rotated log, directly:xzgrep error app.log.xz.xz -t file.xztests the file and prints nothing if it’s intact.
If you run unxz on a .tar.xz file, you get a .tar file. That’s fine: extract it with tar -xf file.tar. It’s simply quicker to let tar do both steps.
6. Extract tar.xz Files on macOS
The tar command that comes with macOS (bsdtar) reads XZ archives without anything extra. Open Terminal and run the same command as on Linux:
tar -xf archive.tar.xz
In Finder, you can usually just double-click the file and Archive Utility unpacks it next to the original. macOS doesn’t include the xz command, so for a plain .xz file install it with Homebrew (brew install xz) and then use unxz as shown above. Free apps such as The Unarchiver and Keka also open both formats.
7. Extract tar.xz Files on Windows
Windows 11 extracts .tar.xz, .txz, .tar.gz, .7z and .rar files in File Explorer without extra software (the feature arrived with the 2023 updates and uses the open-source libarchive library). Right-click the file, choose Extract All…, pick a folder and click Extract.
On Windows 10, or if File Explorer is slow with a large archive, use the free 7-Zip:
- Right-click the
.tar.xzfile and choose 7-Zip > Extract Here. This removes the XZ layer and leaves a.tarfile. - Right-click the new
.tarfile and choose 7-Zip > Extract Here (or Extract to “folder\”) again to get the files.
7-Zip needs two steps because XZ and tar are two separate layers. For a plain .xz file, the first step is all you need. If you use WSL, you can also run the Linux tar -xf command from your WSL shell.
Create a tar.xz File
To go the other way, use -c (create) with -J for XZ compression:
tar -cJf backup.tar.xz /path/to/folder
To compress a single file, run xz file (add -k to keep the original). XZ is slow at its default level on large data. You can pass options through the XZ_OPT variable, for example -T0 to use all CPU cores:
XZ_OPT='-T0' tar -cJf backup.tar.xz /path/to/folder
For more archive examples, see the tar command with practical examples and the tar cheat sheet. If you care more about speed than size, Zstandard (.tar.zst) compresses much faster than XZ.
Troubleshooting Common Errors
xz: Cannot exec: No such file or directory
tar uses the xz program to decompress, and it isn’t installed. Install xz-utils (Debian, Ubuntu) or xz (Fedora, RHEL, Arch), as shown in the install section.
gzip: stdin: not in gzip format
You used the gzip option -z (for example tar -xzf) on an XZ file. Drop the compression option and run tar -xf, which works for every format.
tar: This does not look like a tar archive
The file is a plain .xz file, not a tar archive. Decompress it with unxz file.xz instead of tar.
Unexpected end of input / Unexpected EOF in archive
The file is incomplete, usually because a download was cut off. Run xz -t file.tar.xz to confirm, then download it again and compare its checksum (sha256sum file.tar.xz) with the one the website publishes.
File format not recognized
The file isn’t XZ data, even though its name ends in .xz. Often it’s an HTML error page saved by a broken download link. Check it with file and download it again.
FAQ
How do I extract a tar.xz file in Linux?
Run tar -xf file.tar.xz. Add -C /path/to/dir to extract somewhere other than the current directory, and -v to see each file name.
What is the difference between .xz and .tar.xz?
.xz is a single compressed file. .tar.xz is a tar archive, which can hold many files and folders, that has been compressed with XZ. Use unxz for the first and tar -xf for the second.
Do I need the -J option to extract tar.xz?
No. GNU tar and bsdtar detect XZ compression on their own, so tar -xf works. -J is only required when tar reads the archive from a pipe, as in curl -L URL | tar -xJ.
How do I unpack an .xz file and keep the original?
Use unxz -k file.xz or xz -dk file.xz. Without -k, the .xz file is deleted after decompression.
What is a .txz file?
It’s a short name for .tar.xz. Extract it the same way: tar -xf file.txz.
Is xz safe to use after the 2024 backdoor?
Yes. The backdoor (CVE-2024-3094) was found only in XZ Utils versions 5.6.0 and 5.6.1, and distributions removed those versions within days in March 2024. Check yours with xz --version, and keep the package updated through your distribution, which also brings later fixes from the 5.8 series.
Conclusion
To unpack a .tar.xz file, run tar -xf file.tar.xz, and add -C to choose the destination. For a plain .xz file, run unxz file.xz, and add -k if you want to keep the compressed copy. List the contents with tar -tf before you extract anything you downloaded. On Windows 11, File Explorer’s Extract All works, and on macOS the built-in tar or a double-click does the job. For other formats, see how to extract tar.gz files, extract tar.bz2 files, or the guide to extracting zip, gz, bz2, 7z, xz and rar files.
How did it make you feel?
