There are various utilities available to create and extract tar archives such as gzip, bzip, xz, etc. The “xz” is the most popular and frequently used algorithm for compressing or decompressing files and folders. It is a good technique to reduce the file size without influencing its information.
This article will demonstrate the methods to extract tar.xz files in Linux. The content for the post is as follows:
- Installation of the xz Utility
- How to use xz for Compressing and De-Compressing?
- How to Extract tar.xz Files In Linux?
Let’s start with the first outcome.
Installation of the xz Utility on Linux
The “xz” is the built-in utility in most Linux Distributions, but it can be installed if it is unavailable in the operating system. Below are the commands for installation of the xz command in various Linux Distributions.
For Debian/ Ubuntu:
$ sudo apt install xz-utils
For Fedora/RHEL:
$ sudo dnf install xz liblzma-devel
For OpenSUSE:
$ sudo zypper install xz liblzma-devel
You can verify the installation of the “xz” command using the “-V” option with xz command:
$ xz -V
The xz utils 5.2.5 is installed.
How to Use xz for Compressing and De-Compressing?
To use the xz command for compressing the files, check out the below examples of compressing and decompressing the files.
Compressing the File
For compressing any file, use the “-z” option with the xz utility:
$ xz -z itslinuxfoss.txt
The execution of the above command shows that the file “itslinuxfoss.txt” has been compressed.
Let’s verify it using the “ls” command:
$ ls
The file has been compressed.
De-Compressing the File
Similarly, for de-compressing the files, use the “unxz” command in the terminal:
$ unxz itslinuxfoss.txt.xz
The file will be decompressed as seen in the below image:
Let’s move and check the extraction method files in Linux.
How to Extract tar.xz Files In Linux?
Users can also use the tar command for creating and extracting archive files. Let’s implement this in the following examples.
Example 1: Creating tar.xz File
Consider the following output of a directory named “Henry”:
$ ls Henry
To create the archive files in Linux, use the tar command on the .xz extension of the files
$ tar -cJf Henry.tar.xz Henry/
The tar.xz file will be created for Henry Directory.
Example 2: Extracting tar.xz File
Let’s extract the “Henry.tar.xz” created in the above example. To do so, use the “x” and “f” flag with the tar command:
$ tar -xf Henry.tar.xz
The file will be extracted, to check it, let’s run the “ls” command in the terminal:
$ ls
The above image shows that the “Henry” directory has been extracted.
Example 3: Extracting tar.xz File to Particular Directory
You can also extract the tar.xz file in a specific directory such as “Desktop”. To do this, use the “C” flag and type the path to the directory:
$ tar -xvf Henry.tar.xz -C ~/Desktop/
The file has been extracted in the “Desktop” directory.
Let’s check our extracted folder in the “Desktop” directory:
$ ls Desktop/Henry
The file has been extracted in the “Desktop” directory.
Example 4: Listing the tar.xz File Without Extracting
Another facility that the tar command provides is to list down the output of the archive file. To do so, use the “t” and “f” flags, then type the name of the archive file:
$ tar -tf Henry.tar.xz
The content of the “Henry.tar.xz” has been listed.
Example 5: Extracting Files of Particular Pattern
In some cases, one archive file contains multiple files with different extensions such as “.txt”, “.sh”, etc. you can specify the particular pattern of the file extension to be extracted from archive files. We have three types of files, “.sh”, “.txt”, and “.out”, that can be seen in the below image:
$ tar -tf Henry.tar.xz
Let’s extract all “.txt” files in the “Henry.tar.xz”. To do so, use the “–wildcard” option and then type the expression:
$ tar -xf Henry.tar.xz --wildcards '*.txt'
All the “.txt” files will be extracted. To verify it, let’s run the ls command in the extracted directory:
$ ls Henry
That’s how “tar.xz” files can be extracted.
Conclusion
To extract the tar.xz files, use the tar command in the terminal. There are various options available that can be used when extracting it. Users can use these options for extracting the file in a particular directory, to list down the content without extracting it, or extract the specific type of file present in the tar.xz file. This write-up has illustrated the method to compress or decompress the file using the xz utility and extract the tar.xz file.