How to Extract (Unzip) tar.xz File

Published on

4 min read

Unzip Tar Xz File

The tar command allows you to create and extract tar archives. It supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress.

Xz is a popular algorithm for compressing files based on the LZMA algorithm. By convention, the name of a tar archive compressed with xz ends with either .tar.xz or .txz.

This article explains how to use the tar command to extract (or unzip) .tar.xz or .txz archives.

Extracting tar.xz File

The tar utility is pre-installed by default on all Linux distributions and macOS.

To extract a tar.xz file, invoke the tar command with the --extract (-x) option and specify the archive file name after the -f option:

tar -xf archive.tar.xz

tar auto-detects compression type and extracts the archive. The same command can be used to extract tar archives compressed with other algorithms, such as .tar.gz or .tar.bz2 .

If the command-line is not your thing, you can use the GUI File manager. To extract (unzip) a tar.xz file simply right-click the file you want to extract and select “Extract”. Windows users need a tool named 7zip to extract tar.xz files.

For more verbose output, use the -v option. This option tells tar to display the names of the files being extracted on the terminal.

tar -xvf archive.tar.xz

By default, tar extracts the archive contents in the current working directory . To extract archive files in a specific directory, use the --directory (-C).

The following example shows how to extract the archive contents to the /home/linuxize/files directory:

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

Extracting Specific Files from a tar.xz File

To extract a specific file(s) from a tar.xz file, append a space-separated list of file names to be extracted after the archive name:

tar -xf archive.tar.xz file1 file2

When extracting files, you must provide their exact names including the path, as printed when the tar is invoked with the --list (-t) option.

Extracting one or more directories from an archive is the same as extracting multiple files:

tar -xf archive.tar.xz dir1 dir2

If you try to extract a file that doesn’t exist in the archive, an error message similar to the following will be shown:

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

The --wildcards option allows you to extract files from a tar.xz file based on a wildcard pattern. The pattern must be quoted to prevent the shell from interpreting it.

For example, to extract only the files whose names end in .png, you would use:

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

Extracting tar.xz File from stdin

When extracting a compressed tar.xz file by reading the archive from standard input (usually through piping), you must specify the decompression option. The -J option tells tar that the file is compressed with xz.

In the example below we are downloading the Linux kernel using the wget command and pipe its output to the tar command:

wget -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.3.tar.xz -O - | sudo tar -xj

If you don’t specify a decompression option, tar will show you which option you should use:

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

Listing tar.xz File Content

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

tar -tf archive.tar.xz

The output will look something like this:

file1
file2
file3

If you add the --verbose (-v) option, tar will print more information, such as owner, file size, timestamp ..etc:

tar -tvf archive.tar.xz
-rw-r--r-- linuxize/users       0 2020-02-15 01:19 file1
-rw-r--r-- linuxize/users       0 2020-02-15 01:19 file2
-rw-r--r-- linuxize/users       0 2020-02-15 01:19 file3

Conclusion

tar.xz file is a Tar archive compressed with xz. To extract a tar.xz file, use the tar -xf command, followed by the archive name.

If you have any questions, please leave a comment below.