extract tar.xz files on linux 1

How to Extract tar.xz Files on Linux

There are so many compression archives out there available on different operating systems. Compression archives are an excellent way to reduce file sizes before storing them for later use. In this article, we will discuss how to extract tar.xz files on Linux.

Compression: Basics

Why do we need to compress files? Basically, files are compressed to reduce their size, but why do we need to reduce file sizes? Two main reasons why files are being compressed are so that they take less space and they are faster to download or upload.

Archive File: Basics

An archive file is composed of a single or multiple files and metadata. Archive files are also used for storing multiple files for ease of storage and portability. Managing a single file is a lot easier than managing multiple files in different locations.

TAR.XZ: Basics

Although the tar.xz file type is rather uncommon these days, it has been around since the 1980s. It’s a compression archive also similar to the likes of RAR, GZIP, WINZIP, WINRAR files.

It is used to compress files and folders to save space. It is also easier or faster to transmit files if they are smaller. Smaller file size also means less network traffic for uploading or downloading and less expensive to store them for safekeeping.

TAR.XZ file is a lossless data compression file format, meaning information is intact despite the reduction in file size. This file format is created through the “tar” command on Linux operating systems, hence the name TAR.XZ. The “XZ” part is from 2005, a compression utility that was developed to be an upgrade for the more familiar ZIP format.

It may not be comparable to other compression archives in terms of popularity. Still, this format remains one of the most efficient open-source compression available on the credit of its lossless attribute. That said, you are assured of finding no drop in quality upon extraction, no matter what you compress in this format.

Extract tar.xz Files on Linux

By default, tar utility comes pre-installed on all Linux distros and Mac OS. You can also use the tar command to extract tar.xz files. Along with the tar command, the XZ Utils package also comes pre-installed most of the time. But if ever it isn’t, installing it should be the first thing we need to do.

Install XZ Utils Package on Linux

The XZ format is supported on almost all operating systems, including Windows, MacOS, FreeBSD, Linux, and more. To install the XZ Utils package on Linux distros like Fedora, Ubuntu, or OpenSuse, you can also use the commands below.

Install XZ Utils Package on Ubuntu/Debian

$ sudo apt install xz-utils

XZ Utils Package Installation for RedHat/Fedora

$ sudo dnf install xz liblzma-devel

Install XZ Utils Package on Opensuse

$sudo zypper install xz liblzma-devel

Compress and Decompress .xz Files on Linux

Since we are now done installing the XZ Utils package, let’s go ahead and try to compress a file. Use the command below to compress a file with the .xz format. For our example, we will be compressing a file named Howfast.ogg. 

Syntax:

$ xz -z <FileName>

Example:

$ xz -z Howfast.ogg
extract tar.xz files on linux

As you can see in the screenshot above, upon running the command, the file itself gets converted into a compressed file, from “Howfast.ogg” to “Howfast.ogg.xz”.

Now let’s try to extract the compressed file we have just created. Use the command below to extract the file.

Syntax:

$ unxz <Filename>

Example:

$ unxz Howfast.ogg.xz
extract tar.xz files on linux

One thing to note is that Linux’s file system is case-sensitive. As you can see above, the filename could not be recognized since the letter “f” was capitalized. Upon running the command to extract a .xz file, it does not create a new file, and it simply converts the compressed file into an uncompressed file.

Use xz Utility with tar Archiving Utility and Get tar.xz Files on Linux

Create tar.xz Files

Using the tar archiving utility with the xz utility, we can create archive files in .xz format. You can use the command below to create a new tar.xz format archive file. For our example, we will be naming our new .xz file as “sample_folder.tar.xz”.

Syntax:

$ tar -cJf <tar.xz Filename> <Folder to be archived>

Example:

$ tar -cJf sample_folder.tar.xz sample_folder
tar files

In this screenshot, before running the command above, we created a new folder, named it as sample_folder, and copied the two sample files into it. As you can see, the sample_folder contains two files, which are Howfast.ogg and JoshWoodward-Swansong.ogg. Upon running the command, it creates a new tar.xz file, which we named sample_folder.tar.xz, and it contains the sample_folder along with the two sample files.

Extract tar.xz Files

Run the command below to extract a tar.xz files on Linux.

Syntax:

$ tar -xvf <tar.xz Filename>

Example:

$ tar -xvf sample_folder.tar.xz
extract tar.xz files on linux

Here you can see the sample_folder already exists, so when we ran the tar command above, the sample_folder got overwritten. The options -xvf for the tar command overwrites files, unless you use the -k option, then it will keep the old files. Also, if you have noticed, since we used the -v option, the command became verbose, and you could see what has been decompressed.

Extract tar.xz Files to Another Directory

To extract your tar.xz file to another location, you can use the below command.

Syntax:

$ tar -xvf <tar.xz Filename> -C </Directory/Path/>

Example:

$ tar -xvf sample_folder.tar.xz -C ~/Desktop/
extract tar.xz files on linux

Sometimes you may get some errors from the order of the options that you use. In this screenshot, you can see that we got an error just because we typed in -xfv instead of -xvf.

View Contents of tar.xz Files without Extracting

You can use the command below to list down the contents of a tar.xz file without extracting them.

Syntax:

$ tar -tf <tar.xz Filename>

Example:

$ tar -tf sample_folder.tar.xz
sample folder 2

Here you can see that sample_folder.ta.xz contains two files which are Howfast.ogg and JoshWoodward-Swansong.ogg.

Extract Specific Files from tar.xz Archives

To extract a specific file from a tar.xz format archive, the command below is what you will need to use.

Syntax:

$ tar -xvf <tar.xz Filename> <Path/Filename>

Example:

$ tar -xvf sample_folder.tar.xz sample_folder/Howfast.ogg
sample folder

As you can see in this screenshot, the option to execute in verbose shows you exactly what file has been extracted, which is the Howfast.ogg file in a folder named sample_folder.

Extract Files Using the –wildcard Option

Alright! Now, lets try using the --wildcards option to extract files and follow a certain pattern. For our example, we will extract all .txt files from our tar.xz file. 

Create New tar.xz File and Include .txt Files

But before that, let us first create a new tar.xz file with sample .txt files in it. Use the command below to create a new tar.xz file. This time we will be running the command in verbose.

Syntax:

$ tar -cJvf <tar.xz Filename> <Folder to be archived>

Example:

$ tar -cJvf sample_folder_with_txt_files.tar.xz sample_folder
extract tar.xz files on linux

Here you can see that we already added three .txt files in the sample_folder, along with them are the two other .ogg sample files. Upon running the command above, you can see a new tar.xz file named sample_folder_with_txt_files.tar.xz has been created. Files included in our new tar.xz files is two .ogg files and three .txt files in a folder named sample_folder.

Remove sample_folder and Delete Everything In It

Now, before we try to extract our new tar.xz file using the --wildcards option, let us first delete the sample_folder. Use the below command to delete the folder along with its contents.

Syntax:

$ rm -rv <Folder Name>

Example:

$ rm -rv sample_folder
extract tar.xz files on linux

Here you can see that the folder sample_folder has been deleted along with all the files in it.

Now that we have created a new tar.xz archive file with .txt contents, let us go ahead and try to extract using the --wildcards option. In this example, we will be extracting the .txt files only. This is why we deleted the sample_folder first, so you could clearly see what happens after running the command. Run the command below to extract only .txt files from our tar.xz archive.

Syntax:

$ tar -xvf <tar.xz Filename> --wildcards <’pattern’>

Example:

$ tar -xvf sample_folder_with_txt_files.tar.xz --wildcards ‘*.txt’
extract tar.xz files on linux

You can see in this screenshot that only .txt files have been extracted from the sample_folder_with_txt_files.tar.xz archive.

And that’s about it for this tutorial. We looked at how to extract tar.xz files on Linux, create tar.xz files, install the XZ Utility on different Linux distros, and we also discussed the different ways to use the tar command with the XZ Utility on Linux.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts