How to Extract Tar.xz File on Linux Command Line

How to Extract (Unzip) Tar.xz File on Linux Command Line
How to Extract Tar.xz File on Linux Command Line

Today we learn in this tutorial how to extract (Unzip) Tar.xz Tar.gz File on Linux Command Line Command tarallows you to create and extract archives with tar format. This command also supports various 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 .tar.xz or .txz .

In this tutorial, we will explain how to extract (or unzip) .tar.xz and archives .txz .

How to extract tar.xz . file

Most Linux and macOS distributions come with the tar utility installed by default.

To extract the tar.xz file, Use the command tarwith options --extract ( -x) and specify the archive file name after the option -f:

tar -xf archive.tar.xz 

The command tar automatically detects the 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 you are a Desktop user and still afraid to use the terminal/command line, you can use your File Manager. To extract (unzip) the tar.bz2 file simply right-click the file you want to extract and select “Extract”. Windows users will need a tool called 7zip to extract the tar.xz file.

For more verbose output use options -v. This option tells tar to display the name of the file being extracted in the terminal.

tar -xvf archive.tar.xz 

By default, tar will extract the contents of the archive in the current working directory . Use --directory ( -C) to extract archive files in a specific directory:

For example, to extract the contents of an archive to a directory /home/budi/files, you must type:

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

Extract Specific Files from Tar.xz File File

To extract specific files from the tar.bz2 file, add a list of separated filenames. For example, we want to extract foto201.jpgand foto202.jpg

tar -xf archive.tar.xz foto201.jpg foto202.jpg 

When extracting files, you must provide the exact name, including the path, as printed when option --list ( -t) is used.

Extracting one or more directories from an archive is equivalent to extracting multiple files

tar -xf archive.tar.xz dir1 dir2 

If you try to extract a file that is not in the archive, an error message similar to the following will be displayed:

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

Option --wildcardsallows you to extract files from tar.xz files based on wildcard patterns. The pattern must be quoted to prevent the shell from interpreting it differently.

For example, to extract only files whose names end in .png(image file), you would use:

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

Extract the tar.xz file from stdin

When extracting a compressed tar.bz2 file by reading the archive from standard input (usually via piping), you must specify a decompression option. Option -Jwill tell tarthat the file is compressed with xz.

In the example below, we will download the Linux kernel using the command wget and send the result to command tar:

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

If you do not specify an option to decompress, tarwill show you which option you should use:

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

Create a List of tar.xz files

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

tar -tf archive.tar.xz 

The output will look like this:

file1
file2
file3
 

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

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

Conclusion

In this tutorial we learned How to Extract Tar.xz File on Linux Command Line, The tar.xz file is a Tar archive compressed with xz. To extract the tar.xz file, use the command tar -jffollowed by the file name.

How to Install Visual Studio Code on Debian 10 Buster

How to Add EPEL Repository on Rocky Linux, AlmaLinux and CentOS

Leave a Comment

Your email address will not be published. Required fields are marked *