Read in 4 minutes
How to Extract (Unzip) Tar Gz File
If you are roaming the open source world, chances are you encounter .tar.gz file on a regular basis. Open source packages are generally available to download in .tar.gz and .zip formats.
tar command is used to create tar archives by converting a group of files into an archive. 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 has its name “Tape ARchive”.
Gzip is the most popular algorithm for compressing tar files. By convention, the name of a tar archive compressed with gzip the should end with either .tar.gz or .tgz.
In short, a file that ends in .tar.gz is a .tar archive compressed with gzip.
tar command can also be used to extract tar archives, display a list of the files included in the archive, add additional files to an existing archive, as well as various other kinds of operations.
In this tutorial, we will show you how to extract (or unzip) tar.gz and tgz archives.
Extracting tar.gz File
Most Linux distributions and macOS comes with tar command pre-installed by default.
To extract a tar.gz file use the
-x) operator and specify the archive file name after the
tar -xf archive.tar.gz
The tar command will auto-detect compression type and will extract the archive. The same command can be used to extract tar archives compressed with other algorithms such as .tar.bz2.
If you are a Desktop user and the command-line is not your thing, to extract (unzip) a tar.gz file simply right click the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files.
-v option will make the tar command more visible and print the names of the files being extracted on the terminal.
tar -xvf archive.tar.gz
By default, tar will extract the archive contents in the current working directory. Use the
-C) to extract archive files in a specific directory:
For example, to extract the archive contents to the
/home/linuxize/files directory, you can use:
tar -xf archive.tar.gz -C /home/linuxize/files
Extracting Specific Files from a tar.gz File
To extract a specific file(s) from a tar.gz file, append a space-separated list of file names to be extracted after the archive name:
tar -xf archive.tar.gz file1 file2
When extracting files, you must provide their exact names including the path, as printed by
Extracting one or more directories from an archive is the same as extracting files:
tar -xf archive.tar.gz dir1 dir2
If you try to extract a file that doesn’t exist, an error message similar to the following will be displayed:
tar -xf archive.tar.gz README
tar: README: Not found in archive tar: Exiting with failure status due to previous errors
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
In the following example we are downloading the Linux kernel using the wget command and pipe the tar.gz file to the
wget -c https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.20.10.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 use the
tar -tf archive.tar.gz
file1 file2 file3
If you list the content of the tar.gz file using the
-v) option, then tar will print more information such as owner, file size, timestamp ..etc:
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
You can also extract files from a tar.gz file based on a wildcard pattern, by using the
--wildcards switch and quoting the pattern to prevent the shell from interpreting it.
For example, to extract files whose names end in
tar -xf archive.tar.gz --wildcards '*.js'
By now you should have a good understanding of how to extract tar.gz archives. You can also read our article about How to Create and Extract Archives Using the Tar Command in Linux.