en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. What is Tar.gz

What is Tar.gz

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

Linux Tutorial Sep 29, 21 by Nisal N 6 min Read
What is Tar.gz

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

Basically, the “tar” command was designed specifically 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 is ending with the .tar.gz then the .tar archive is compressed using the gzip. Also, you can use the tar command for extracting the tar archives that will help in displaying the list of the files that have been archived using the “tar” command.

In this article, we will be focusing on the working of the tar and gzip command and how its work differently with the help of various options.

Extracting tar.gz File

By default, the tar command comes pre-installed in most of the Linux distributions and macOS. 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 will detect the compression type automatically and then start extracting 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 that 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, then you can use the 7zip for extracting the tar.gz files.

In the below example. We have used the “-v” option for making 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

Whenever you are extracting the archive files using the tar command then 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 then you must specify their accurate names along with the path so they can be printed by using the “--list" or "-t” option.

If you want to extract one or more directories from the archive then you can use the following syntax of the command. It is somehow similar to the way 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 then 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. You need to use the “--wildcard” option along with the pattern in quotes that will prevent the shell from interpreting it. You can consider the following example, where we 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, then 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 below example we are trying to downloading the blender sources with the help of the wget command and then piping the output to the tar command as shown below.

wget -c https://download.blender.org/source/blender-2.80.tar.gz -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, then 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-

file1
file2
file3

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

Output- 

-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

Conclusion

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

We hope that you got a proper understanding of how the tar command works in extracting files and directories with the help of various options.

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment