How to Extract/Unzip Tar Gz File in Linux

How to Extract (Unzip) Tar Gz File in Linux

In this article, we will explain how to extract tar.gz files using the tar command.

Tar is an abbreviation for tape archive, and it is one of the most commonly used commands for dealing with compressed archive files.

Gz is an abbreviation for gunzip. It is a particular compression algorithm. Most Linux distributions have the tar command pre-installed. The tar program compresses and extracts files using various methods.

Tar supports a broad range of compression methods, including gzip, bzip2, xz, lzip, and others.

Extract a tar.gz file quickly

In this section, we’ll show you a simple method to extract any tar.gz file. In the later sections, you will get to know the tar command in a little bit more detail.

To extract all the files inside of a tar.gz file, use the -xf flag with the tar command:

tar -xf file.tar.gz

Here, x stands for extract and f stands for the archive file. The tar command detects the compression type automatically and extracts it. You don’t need to specify the file/compression type to extract. For example, you could extract a tar.gz file with the same command.

Alternatively, you can also use the graphical user interface (GUI) instead of the command line. Just right click on the tar gz archive file you want to extract and click on the Extract option.

Basic usage of tar command

The basic syntax of the tar command is as follows:

tar [options] [file]

The tar command has a plethora of options in the help menu. You can access it by typing in tar --help.

We’ll be using the main operation mode most of the time. This mode has some basic options for creating and extracting archives. Below are three of these options:

-c, --create               create a new archive
-t, --list                 list the contents of an archive
-x, --extract, --get       extract files from an archive

You’ve already seen the usage of the -x flag for extracting an archive. Let’s take a look at some other options now.

Listing the contents of a tar gz archive

If you just wanted to take a look at the contents of an archive, you would use the -t or --list flag:

tar -tf compressed_file.tar.gz

We can get more details about the archive using the -v or --verbose flag with the command. The output will include file/folder details such as owner, permissions, etc. Let’s see it in action:

tar -tf compressed_file.tar.gz
-rw-r--r-- root/root 3153920 2021-10-15 21:55 file
-rw-r--r-- edxd/edxd 1048576 2021-10-15 21:54 file.txt
-rw-r--r-- root/root 2097152 2021-10-15 21:54 file.log

As you can see, the file permissions and owner along with the file size is shown in the output.

Extract specific files/folders from a tar gz archive

Imagine you need a specific file from a large archive. In this case, you might want to only extract that specific file from the archive. This can be done by simply specifying the filename (and file path) followed by the extract command.

Let’s see how to extract only the required files:

Check what files are in the working directory with the ls command:

compressed_file.tar.gz file.tar.gz tutorial.firstpage.php

I have already shown you the content of the compressed_file.tar.gz in the previous section (listing files). Now let’s extract file and file.log from this archive. We’ll be using the following command:

tar -xf compressed_file.tar.gz file file.log

Here, we used the -xf flag with the command to extract the compressed_file.tar.gz archive. We also mentioned file and file.log indicating which files to extract. Now let’s check our working directory again with the ls command:

compressed_file.tar.gz file file.log file.tar.gz tutorial.firstpage.php

As you can see, the file and file.log have been extracted from the compressed_file.tar.gz archive. You can also extract specific directories/folders by using this method. The command would look like the following:

tar -xf archive.tar.gz dir1 dir2 dir3…

Here, dir1, dir2, and dir3 are the names of the directories/folders you want to extract from the archive.

Use wildcards for specific file extensions

Let’s say you want to extract all the files that have the same extension from inside of an archive. In this case, you could use a wildcard like *.extension format. Here, you can use your specific extension. For example, if you wanted to extract all the text files, you would use *.txt. Let’s see this in action:

First, let’s take a look at our current directory:

compressed_file.tar.gz file.tar.gz test.tar.gz

Let’s take a look at the files inside the test.tar.gz archive:

tar -tf test.tar.gz

Now, we have two text files that end in .txt extension. Let’s extract these two files with the *.txt wildcard. We also need to use the --wildcards flag to enable the wildcard option:

tar -xf test.tar.gz --wildcards *.txt

Now let’s check our current directory to see if it worked:

compressed_file.tar.gz file.tar.gz num.txt pr_ex_creator.txt test.tar.gz

From the output, we can see that those two text files were extracted. This is how you can use wildcards to extract all the files with a specific extension.

Change the location of extraction

If you want to change the location where the extracted files will occupy, you can use the -C flag and specify the location of your choice. Here’s an example of how to do it:

tar -xf compressed_file.tar.gz -C /home/new/

Here, after the -C flag, I’ve specified the path for saving the extracted files. The path /home/new/ is marked in color. Let’s take a look at the contents of the directory:

ls /home/new/
file file.log file.txt

As you can see, the compressed_file.tar.gz file extracted in the /home/new/ location.


We demonstrated how to extract a tar.gz archive in this lesson. In summary, you can extract the archive with the tar command with the -xf parameter. When possible, utilize the graphical user interface if you’re still unclear how to use the tar command.

Some examples may be found in the tar command’s help menu. If you have any queries, please leave them in the comments section.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like