How to Extract (Unzip) Tar Gz File
Updated on
•6 min read

If you are using open-source software, chances are you encounter .tar.gz files on a regular basis.
Open-source packages are often distributed in .tar.gz and .zip formats.
Tar archives, often referred to as tarballs, are created by converting a group of files into an archive. These archives are mostly used for backup or software distribution purposes. In Linux and Unix systems, you can create tar archives using the tar
command. It supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. Tar was originally designed for creating archives to store files on magnetic tape which is why it is named “Tape ARchive”.
Gzip is the most popular algorithm for compressing tar files. By convention, the name of a tar archive compressed with gzip should end with either .tar.gz or .tgz.
In short, a file that ends in .tar.gz is a .tar archive compressed with gzip.
Besides creating new archives, the tar
command can also be used for various other operations, including extracting tar archives, displaying a list of files included in the archive, and adding additional files to an existing archive.
This article will teach you how to extract (or unzip) tar.gz and tgz archives.
Extracting tar.gz File
The tar
command is pre-installed by default on most Linux distributions and macOS.
To extract a tar.gz file, use the --extract
(-x
) option and specify the archive file name after the f
option:
tar -xf archive.tar.gz
The tar
command will auto-detect the compression type and extract the archive. You can use the same command to extract tar archives compressed with other algorithms such as .tar.bz2
.
The -v
option makes the tar
command more visible and prints the names of the files being extracted on the terminal.
tar -xvf archive.tar.gz
By default, tar
extracts the archive contents in the current working directory
. Use the --directory
(-C
) option to specify a directory in which you want to unpack the archive.
For instance, to extract the archive contents to the /home/linuxize/files
directory, you can use the following command:
tar -xf archive.tar.gz -C /home/linuxize/files
If you’re a desktop user and prefer not to use the command-line interface, you can use your File manager instead. To extract (unzip) a tar.gz file, right-click on the file you want to extract and select “Extract”.
On Windows computers, you can use a software tool called 7zip to extract tar.gz files.
Extracting Specific Files from a tar.gz File
To extract specific files from a tar.gz archive, include a space-separated list of the file names after the archive name:
tar -xf archive.tar.gz file1 file2
When extracting files, you must provide their exact names along with the path, as listed by tar --list
(tar -t
).
To extract one or more directories from an archive, you can follow the same process as extracting individual files:
tar -xf archive.tar.gz dir1 dir2
If you attempt to extract a file that does not exist, you will see an error message similar to the one below:
tar -xf archive.tar.gz README
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors
You can also extract files from a tar.gz file based on a wildcard pattern, by using the --wildcards
option and quoting the pattern to prevent the shell from interpreting it.
For example, to extract files whose names end in .js
(Javascript files), you would use:
tar -xf archive.tar.gz --wildcards '*.js'
Extracting tar.gz File from stdin
If you are extracting a compressed tar.gz file by reading the archive from stdin (usually through a pipe), you need to specify the decompression option. The option that tells tar to read the archives through gzip is -z
.
In the following example we are downloading the Blender
sources using the wget
command and pipe its output to the tar
command:
wget -c https://download.blender.org/source/blender-4.0.2.tar.xz -O - | sudo tar -xz
If you don’t specify a decompression option, tar
will indicate which option you should use:
tar: Archive is compressed. Use -z option
tar: Error is not recoverable: exiting now
Listing tar.gz file
To list the content of a tar.gz file, invoke the tar
command with the --list
(-t
) option:
tar -tf archive.tar.gz
The output will look something like this:
file1
file2
file3
If you add the --verbose
(-v
) option, tar
will print additional information such as file permissions, ownership, size, and timestamp.
tar -tvf archive.tar.gz
-rw-r--r-- linuxize/users 0 2023-12-15 01:19 file1
-rw-r--r-- linuxize/users 0 2023-12-15 01:19 file2
-rw-r--r-- linuxize/users 0 2023-12-15 01:19 file3
Conclusion
tar.gz file is a Tar archive
compressed with Gzip. To extract a tar.gz file, use the tar -xf
command followed by the archive name.
If you have any questions, please leave a comment below.