How to Extract (Unzip) Tar Gz File

Introduction

If you spend most of your time in the open-source community, you're bound to come across .tar.gz files. Open-source packages are typically available in .tar.gz and .zip formats for download.

By transforming a set of files into an archive, the tar command is used to produce tar archives. Gzip, bzip2, lzip, lzma, lzop, xz, and compress are just a few of the compression applications it supports. Tar was created with the intention of creating archives to store files on magnetic tape, hence the name "Tape ARchive."
The most often used algorithm for compressing tar files is Gzip. A tar archive compressed with gzip should end in either .tar.gz or .tgz, according to the convention.

In a nutshell, a .tar.gz file is a .tar archive compressed with gzip.

The tar command can also be used to extract tar archives, show a list of the contents contained in the archive, and add additional files to an existing archive, among other things.

In this tutorial, you will use extract (Unzip) tar.gz file. We will also address a few FAQs on how to extract (Unzip) tar.gz file.

Extract tar.gz File

The tar command is pre-installed in most Linux distributions and macOS.

Use the --extract (-x) option and the archive file name after the f option to extract a tar.gz file:

tar -xf archive.tar.gz

The tar command will automatically detect the compression type and extract the archive. The same command can be used to extract tar archives that have been compressed using various techniques, such as .tar.bz2.

If you're a desktop user who prefers not to utilize the command line, you can use your file manager instead. To extract (unzip) a tar.gz file, simply right-click on it and choose "Extract." To extract tar.gz files, Windows users will need to use the 7zip program.

The -v option makes the tar command more visible on the terminal and prints the names of the files being extracted.

tar -xvf archive.tar.gz

By default, tar extracts the contents of the archive into the current working directory. To extract archive files in a specific directory, use the --directory (-C) option:

To extract the contents of an archive to the /home/vega/files directory, for example, use:

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

Extract Specific Files from a tar.gz File

You need to append a space-separated list of file names to be extracted after the archive name to extract a specific file(s) from a tar.gz file:

tar -xf archive.tar.gz file1 file2

You must supply their precise names, including the path when extracting files, as printed by --list (-t).

It's similar to extracting files from an archive to extract one or more directories:

tar -xf archive.tar.gz dir1 dir2

You will get an error message that looks like this, if you try to extract a file that doesn't exist,

tar -xf archive.tar.gz README
Output

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

By using the --wildcards option and quoting the pattern to prevent the shell from interpreting it, you can extract files from a tar.gz file based on a wildcard pattern.

To extract files ending in .js (Javascript files), for example, you would use:

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

Extract tar.gz File from stdin

You must specify the decompression option if you are extracting a compressed tar.gz file by reading it from stdin (often over a pipe). The -z option instructs tar to read the archives using gzip compression.

We'll use the wget command to obtain the Blender sources and pass the result to the tar command in the following example:

wget -c https://download.blender.org/source/blender-2.80.tar.gz -O - | sudo tar -xz

If you don't specify a decompression option, tar will recommend the following:

Output

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

List tar.gz File

Use the --list (-t) option to list the contents of a tar.gz file:

tar -tf archive.tar.gz

You will get an output like below:

file1
file2
file3

tar will print more information if you use the --verbose (-v) option, such as the owner, file size, timestamp, and so on:

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

FAQs to Extract (Unzip) Tar Gz File

Can I extract a tar.gz file without installing additional software?

No, you need to have a utility like tar installed on your system, as it is required to extract the contents of a tar.gz file.

What is the command for extracting a tar.gz file?

The command for extracting a tar.gz file is tar -xzf filename.tar.gz.

Can I specify a destination directory while extracting the tar.gz file?

Yes, you can specify a destination directory using the -C flag followed by the desired directory path.

Can I extract only specific files from a tar.gz file?

Yes, you can extract specific files by providing their names or patterns as arguments to the tar command.

How do I view the contents of a tar.gz file without extracting it?

To view the contents of a tar.gz file without extracting, you can use the tar -tzf filename.tar.gz command.

Will extracting a tar.gz file overwrite existing files?

Yes, if a file with the same name already exists in the extraction directory, it will be overwritten during the extraction process.

Is it possible to preserve file permissions while extracting a tar.gz file?

Yes, you can use the tar -xzf filename.tar.gz --preserve-permissions command to preserve file permissions during extraction.

Conclusion

A tar.gz file is a Gzip-compressed Tar archive. You can use the tar -xf command followed by the archive name to extract a tar.gz file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.