Tar allows one to extract and create tar archives. It maintains a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz, and compress. The Xz algorithm is one of the most popular compression methods based on the LZMA algorithm. The name of a tar archive compressed with xz concludes with the string ‘tar’ and contains the string ‘xz.’The article explains how to use the tar command to unzip archives and use the unzip command.
Extracting tar.xz File
The tar utility is included in all Linux distros and macOS by default. To extract a tar.xz file, create a subdirectory in the current directory and input the tar command followed by the -x option.
$ tar –xf myfolder.tar.xz
Tar extracts archive by identifying the archive type. The same command is used to determine the archive type, such as .tar, .tar.gz, or .tar.bz2.For more robust output, use the -v flag. This option instructs tar to list the names of the files stored on the hard drive.
$ tar –xvf myfolder.tar.xz
For the automated extraction, archive contents are extracted from the working directory itself. To properly extract archived files, use the – directory parameter (-C)
Step-by-step guide to extracting the archive to the /home/test/files directory.
$ tar –xf myfolder.tar.xz -C /home/test/files
- How To Archive and Compress Files on Linux
- How to Install Eclipse IDE on Ubuntu 18.04
- How To Encrypt File on Linux
Extracting Specific Files from a tar.xz File
To extract the files from a tar.xz file, append space-separated names of files to be extracted to the end of the archive name:
$ tar –xf myfolder.tar.xz file1 file2
If you are extracting files, you must supply each file’s exact names, including where it was found. Extracting directories from an archive is similar to extracting files from an archive:
$ tar –xf myfolder.tar.xz folder1 folder2
If you attempt to extract a file that no longer exists, an error message will be displayed.
$ tar –xf myfolder.tar.xz README
No such file was found in the archive. Rejecting as bad because of previous problems. If specifying the —wildcards flag, you can extract files from a tar.xz file based on a wildcard pattern. The pattern must be quoted for it to be analyzed. For example, to extract only PNG ZIP files, you would use:
$ tar –xf myfolder.tar.xz —mycards '*.png'
Extracting tar.xz File from the 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 instructs tar that the file is compressed with the xz file format. We can use wget to download the Linux kernel using wget, and then we can use tar to extract the Linux kernel.
$ 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 the decompression format, the tar command will show you the appropriate format.
Tar: Archive is compressed. Use -J option
tar: Error is not recoverable: exiting now
Listing tar.xz File Content
To list the contents of a tarball, use the show the -t command.
$ tar –tf myfolder.tar.xz
The resulting outputs will look similar to this.
myfile1 myfile2 myfile3
When providing —verbose (-v), tar prints more information, such as file size and owner.
$ tar –tvf myfolder.tar.xz -rw-r—r— test /user 0 2020-02-15 01:19 myfile1 -rw-r—r— test /user 0 2020-02-15 01:19 myfile2 -rw-r—r— test/user 0 2020-02-15 01:19 myfile3
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.