Linux offers a flexible command to help you get along with the zipping files. It is important when handling large files that need to be shared over the network using SSH. Make sure to use tar commands. You can use the tar gz command for creating tar archives that will convert a group of files into an archive. The tar command supports a wide variety of compression programs such as gzip, bzip2, xz, and compress. 

The tar command was explicitly designed for generating the archives for storing the files on magnetic tapes. This is why it was named the "Tape ARchive". Further, for compressing the tar files, Gzip is the commonly used algorithm. If we gzip the tar archives, then the gzip files should enter with either .tar.gz or .tz.

If a file ends with the .tar.gz then the .tar archive is compressed using the gzip. Also, you can use the tar Linux command to extract the tar archives that will help display the list of the files that have been archived using the tar command.

In this article, we will focus on working the tar and gzip command and how it works differently with the help of various options.

Extracting tar gz file

The tar command is pre-installed in most Linux distributions and macOS by default. For extracting the tar.gz file, you need to use the --extract or -x option followed by the archive name after the -f option. We have mentioned an example below that will give you an understanding of how the command will work.

tar -xf archive.tar.gz

The tar command automatically detects the compression type and extracts the archived files. You can also use the same command if you want to extract the tar archived compress with the help of another algorithm such as .tar.bz2.

If you do not want to use the command line, you can also use the desktop file manager to extract or unzip the tar.gz files just by clicking the right-click on the folder you want to extract. The folder will get unzipped in the same directory if you do not mention any other directory. If you are a Windows user (Tar Windows), you can use the 7zip to extract the tar.gz files.

In the below example, we have used the -v option to make the tar command more visible and then print the names of the files that have been extracted on the terminal.

tar -xvf archive.tar.gz

When you extract the archive files using the tar gz Linux command, they will be extracted in the current working directory. But if you want to do the extraction in any other directory, then you can use the --directory or -C option to specify the directory where you want to extract the files. You can see the below example-

tar -xf archive.tar.gz -C /home/data_detail/files

Extracting Specific Files from a tar.gz File

If you want to extract specific and multiple files from the tar.gz file, then you need to specify the names of the files separated by spaces after the archive name, as shown below.

tar -xf archive.tar.gz file_name1 file_name2

If you are extracting files, you must specify their accurate names along with the path so they can be printed using the --list or -t option.

If you want to extract one or more directories from the archive, then you can use the following command syntax. It is similar to how you extract the files from the archive.

tar -xf archive.tar.gz dir_name1 dir_name2

If you are trying to extract files that do not even exist, you will get an error, as shown below.

tar -xf archive.tar.gz README

Suppose there is no file with the name README, then you will get the below-mentioned error.

tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

You can also use the wildcard patterns for extracting the files from the tar.gz files. It would be best if you used the --wildcard option and the pattern in quotes that will prevent the shell from interpreting it. You can consider the following example, where we are trying to extract the files having extension as .js.

tar -xf archive.tar.gz --wildcards '*.js'

Extracting tar.gz file from stdin

If you want to extract a compressed tar.gz file by reading the archive from stdin, you need to mention the option for proceeding with decompression. You can use the -z option for telling the tar to read the archives from gzip.

In the following example, we are trying to download the blender sources with the help of the wget command and then piping the output to the Linux gz command as shown below.

wget -c -O - | sudo tar -xz

If you have not mentioned the decompression option, then the tar command will indicate the option that you can use-

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

Listing tar.gz file

If you want to list all the contents of the tar.gz file, you can use the --list or -t option along with the tar command, as shown below.

tar -tf archive.tar.gz

You will get the below-mentioned output-


For getting the verbose output, you can use the --verbose or -v option along with the tar command as shown below and you will get additional information.

tar -tvf archive.tar.gz


-rw-r--r-- linuxize/users 0 2019-02-15 01:19 file1
-rw-r--r-- linuxize/users 0 2019-02-15 01:19 file2
-rw-r--r-- linuxize/users 0 2019-02-15 01:19 file3


Compressing and extracting files is one of the fun actions you can perform in the Linux system using various commands to perform different tasks. If you compress the tar archive using the gzip, the file is a tar.gz. To extract the tar gz file, you need to use the -xf option along with the tar command to specify the extraction. 

We hope that you understand properly how the tar command works in extracting files and directories with the help of various options.

People also read: