Untar tar.gz file: The Only How-to Guide You Will Need

untar tar gz files
untar targz files

If you are someone who works with so many files (especially compressed files), you will come across the tar.gz format very often. This format is the industry favorite because it combines both the “.tar” archive and the “.gz” compression. Combining these both powerhouses gives you an efficient way to bundle and then compress files. Now that we know what the “tar.gz” format means, the next question is how to extract these files. In Linux terminology, we are about to see how to “untar” those files.

What Sets the tar.gz Format Apart

At Unixmen, we take every effort to ensure everyone understands the command and its usage before going head-first into the command and shortcuts. Here is the essential part where we explain what tar.gz means.

  • .tar: The “.tar” file is an archive format that is used to collate multiple files into one single file. This feature becomes very useful in applications such as distribution of software packages.
  • .gz: The “.gz” extension in a file shows that the file has been compressed using the gzip algorithm to compress files and directories. Compression is usually done to make transfer of files easier and faster. One more advantage of compressing a file is that it takes minimal disk space than its original format.

When the .tar and .gz extensions are used, it gives out an output that is both bundled and also compressed.

Some Basic Commands to Untar tar.gz Files

The most commonly used method to untar tar.gz file in a Linux machine is using the “tar” command. There are plenty of tar commands but here is the most frequently used one:

tar -xzf file.tar.gz

You might be familiar with the “tar” command. Let us look at the other options now.

  • -x: Pulls the contents out of the archive
  • -z: Shows that the archive has been compressed with gzip
  • -f: Indicates the name of the file to be extracted.

If you would like to untar tar.gz file named unixmen, the command you have to execute is:

tar -xzf unixmen.tar.gz

This command “untars” the file named unixmen into the current directory.
But what if you want to untar tar.gz file into another directory? By default, the tar command extracts all the contents into the current directory. If you would like the contents to be extracted into another directory, use the “-C” option. The basic syntax for performing this operation is:

tar -xzf unixmen.tar.gz -C /destination/directory/path

Executing this command will untar the contents of the file named unixmen.tar.gz into the directory “destination/directory/path”.

How to View Contents of tar.gz File Without Extracting

Before you untar tar.gz file, if you want to be sure if that is the correct file to “untar”, it is a good idea to view the contents of the file before extracting. The easiest way to do that is via the “-t” option of tar command. Let us look at the basic syntax to view the contents of a tar.gz file without extracting.

tar -tzf FileName.tar.gz

How to Extract a Specific File from a tar.gz File

Now that we have learnt how to view the contents of the file without performing untar tar.gz operation, let us see how we can extract a specific file. Use the following syntax:

tar -xzf CompressedFile.tar.gz FileName1FileName2

Running this command performs the untar tar.gz operation only on the files named FileName1 and FileName2 in the tar.gz file named “CompressedFile”. All other files are untouched.

Common Errors During Untar Operation

  • File not found: If you get the “file not found” error, the most common reason is that you are not in the correct directory. Specify the full path to the .tar.gz file.
  • Permission denied: If you face any permission based errors, provide sudo privileges to your sudo command to run it with administrative privileges.
  • Insufficient disk space: Files are stored in tar format mostly because of space constraints. Before extracting large tarballs, make sure you have enough disk space in the target directory.

Other Alternatives

There are a few alternatives to do untar tar.gz operations through GUI. Many Linux distros now come with graphical tools that let you untar tar.gz files with just a few clicks. Some of them are File managers like Nautilus and Dolphin. Test them out and select the one that suits you the most.

Wrapping Up

Working with .tar.gz files becomes very easier when you know how to perform untar actions on them with different use cases. With the “tar” command you can untar tar.gz files either in bulk or specifically. Go through our other articles about Linux useful commands and master Linux faster.