How to Extract/Unzip .tar.gz Files? [Linux & Windows]



What are tar.gz files?

A .tar.gz file, also known as a tarball, is a special file format comprising a collection of files compressed into a single archive. This particular file format is commonly used in Unix and Linux operating systems.

It is simply created by combining multiple files or directories into a single file and then compressing it using gzip compression.

You might be wondering, where the word tar came from, well the word “tar” was used from Tape Archiver, which was originally used for magnetic tape storage.

Just like the .zip files you may have heard about and even used, .tar files are also used to compress large files into a single package, making it convenient to store and send them through various channels such as emails, servers, etc.

At this moment, a question must have come to your mind, why don’t we simply use the .zip file format then? Why there is a need for a separate file format i.e. “.tar” to compress files?

Difference between .Zip file and .Tar file

To better understand the main difference between these two, the best way is to know by their simple definition.

What is a .tar file?

In the .tar file, you will find there is a collection of directories or files uncompressed. Compressing the files in the collection requires a separate compression utility i.e either gzip or bz2 to compress files that’s main purpose is to compress a .tar into a .tar.gz or .tar.bz2.

What is a .tar.gz file?

From the format name itself, you can easily understand that it combines two files, the TAR file and the GZ file. This combination happens when a TAR archive file has been undergoing compression with the Gzip compression utility and becomes a TAR.GZ file.

What is a GZ File?

When talking about a GZ file, it is created by the Gzip compression utility. When a single file is compressed using the Gzip compression utility, it will be turned into a single GZ file. However, when you compress multiple files with the same utility, it will create multiple individual GZ files.

What is a .zip file?

In a .zip file, you will find a collection of compressed files instead. Unlike the .tar file, the .zip doesn’t require any compression initially and compresses each file within the collection.

.ZIP vs .TAR

Feature.zip Files.tar Files
Compression AlgorithmZIP compressionPrimarily used for archiving (not compression by itself)
Cross-Platform CompatibilityWidely supported on Windows, macOS, LinuxNative support on Unix-like systems (Linux, macOS); requires additional tools on some platforms
Compression LevelGood balance between compression ratio and speedTypically a single-step process for both compression and archiving
File and Directory StructurePreserves both file and directory structuresPrimarily focuses on preserving the directory structure; compression is a separate step when using .tar alone
Naming ConventionUses the .zip file extensionUses the .tar file extension when uncompressed; additional extensions like .tar.gz or .tar.bz2 when compressed
Single/Double Step ProcessTypically a single step process for both compression and archivingArchiving and compression are separate steps when using .tar alone
.Zip Vs .Tar

Both file formats have their applications and depend upon your specific needs, compatibility requirements, and your target platforms.

If you are looking for a format that is widely supported and used, .zip can be the best option. However, if you are specifically working on Unix-like platforms, .tar is commonly used.


How Do I Unzip a .tar.gz File in Linux?

For your information, many Linux distributions such as Ubuntu and MacOS come up with inbuilt tools for Compressing and Extracting .tar and .tar.gz files.

Although for most of the tasks, the tar utility is sufficient you will be also required to have a gzip utility for creating .tar.gz files.

Unzip .tar.gz in Linux using the tar utility

In the Linux operating system, the first thing you need to do is to open the Terminal by pressing the CTRL+ALT+T keywords simultaneously and enter the given command below:

1. Extracting .tar.gz to the current working directory and printing out the output

tar -xf Yourfile.tar.gz

In the above command,

  • The “Tar” command will manipulate tar archives
  • the “-x” option will tell “tar” to extract the files.
  • “-f” specifies the file to be extracted. In the above command, it is followed by yourfile.tar.gz which shows the name of the compressed tarball file.

When you run this command, it will extract the contents of this particular tarball file, i.e “Yourfile.tar.gz” into the current working directory.

Note: The extracted files will be stored in the same directory where the command is executed. And also, do not forget to replace the “Yourfile.tar.gz” with the actual name of your compressed tarball file.

To Printing out the Output, enter the below command:

tar -xvf Yourfile.tar.gz

2. Extracting .tar.gz files to different working directory

tar -xf Yourfile.tar.gz -C /home/user/files

3. Extracting the files from the tar archive that has been compressed using gzip

tar –xvzf Yourfile.tar.gz

  • tar: This is the basic command for working with tar archives.
  • -x: Stands for extract, which means you want to extract files from the archive.
  • -v: Stands for verbose, which provides more details about the extraction process, displaying the names of the files as they are extracted.
  • -z: Indicates that the archive is compressed using gzip.
  • -f: Specifies the filename of the archive.

The command will extract the contents of the archive while providing a verbose output of the process.

4. In case you want to see the list of contents in a .tar file before extracting it, enter the command given below:

tar –tzf Yourfile.tar.gz

5. If you want to put the extracted unzipped files into a specific directory, enter the following commands:

tar –xvzf Yourfile.tar.gz –C /home/user/destination

6. If you want to extract only a specific file or Directory from .tar.gz file

Suppose, you want to extract a particular file say “Yourfile” and a directory say “Directory1” from the .tar.gz file to the current working directory. In this case, the command should be like,

tar -xf Yourfile.tar.gz file1 directory1

All you have to just add a space between the files you want to extract.

Note: Please, mention the exact filename in the command, or else it will show an error. Therefore, verify the file name that is listed in the.tar file. To view the contents of the .tar file you can use the “tar –tzf Yourfile.tar.gz” command.

7. If you want to extract a certain type of extension file or name, use the following commands:

tar -xf Yourfile.tar.gz --wildcards '*.txt'

This command will extract all the .txt extension files from the .tar.gz file.

8. If you want to create a .tar.gz file instead, enter the below command:

tar –cvzf NewFile.tar.gz ~/Documents

This command will create a compressed tar archive of the contents of the ~/Documents directory, i.e. located at /home/user/Documents directory into a single file, NewFile.tar.gz.

9. Add multiple files to a tar file

tar -cvf Yourfile.tar ~/Documents

This command will add files by copying the contents of your documents folder into Yourfile.tar.

Extracting .tar.gz files using the gzip utility

The Gzip command is particularly used for compressing and decompressing files. However, in case you want to extract a .tar.gz file, you need to use the tar command as well as the gzip command in the combination.

Gzip by default, extracts the file in the current directory.

gzip -d yourfile.tar.gz

Here you need to replace yourfile.tar.gz with the actual name of your .tar.gz file and the -d option will tell gzip to decompress the file. After decompressing the gzip file, you will have a tar archive and you can proceed further by extracting the tar archive using the tar command.

Extracting .tar.gz files using stdin

If you have a tar.gz file that you want to extract from standard input (stdin), you can use the tar command with the -z option for decompression.

Let’s understand this with an example. Suppose we need to download a file from the URL ‘www.wpoven.com/yourfile‘. In this case, we can use the wget command and pipe its output to the tar command.

wget -c https://Wpoven.com/Yourfile.tar.gz -O - | sudo tar -xz


Unzip a tar gz file Windows 10

For Windows 10 Users, you might be surprised to know just like macOS and Linux operating systems Windows also provides you the utility.

No doubt, Windows provides a more convenient and easier-to-use Graphic user Interface for unzipping and extracting .zip files but for tar files, you need to have a command line and for that command prompt is available.

Unzip .tar.gz file using the command prompt in Windows

Command prompt is available in every version of Windows devices regardless of what version you are using. All you have to open the command prompt by following the below steps

  • Press the Windows + R buttons on your keyboard
  • This will launch a small box for Run Commands.
Open Cmd
Open cmd
  • Enter CMD in the box and press enter
  • It will open the Command Prompt Screen and Run as Administrator.
Command Prompt

And enter the following commands.

To extract a .tar.gz file to its current working directory enter the below commands and hit the “Enter” button.

tar -xvzf Yourfile.tar.gz

If you want to extract.tar.gz file from the source path to the destination path, enter the following commands:

tar -xvzf C:\PATH\TO\SOURCE\YourFile.tar.gz -C C:\PATH\TO\DESTINATION

You also might have noticed that the commands in both the operating system i.e Windows and Linux are nearly the same.

Alternatively, if you are not familiar with the command line and want to use a way simpler and easy method go for third-party tools.

There are various tools available some of them are Free and some of them offer free trials such as WinZip. But they are lightweight and worth trying.


Troubleshoot Errors While Extracting .tar.gz file

It is also possible that at some point in time or while entering commands you might encounter some errors. To help you out in this regard, here are some of the troubleshooting steps you can follow to these some common errors:

1. If the output shows an error message:

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

Solution: If you receive such an error message, it simply implies that you have missed “-z” in the entered command.

for example, if you have entered:

wget -c https://WPOven.com/Yourfile.tar.gz -O - | sudo tar -x

You have to mention a particular decompression option such as gzip (-z) in the above tar command, i.e. “Sudo tar -xz” on the far right.

2. If the out shows an error message:

tar: FILE: Not found in filename
tar: Exiting with failure status due to previous errors

It means either you have entered the wrong filename or the file doesn’t exist in the .tar.gz file.

Solution: You need to cross-check the file name, and its spelling and make sure it is available in the .tar.gz file.


Summary

This ultimate guy helps you How to unzip or extract .tar.gz file using the tar as well as gzip utility tool and also troubleshoot errors while using them. After reading this post thoroughly you are now ready to unzip any tar.gz file on any platform whether it is Linux, macOS, or Windows using the command line easily.

If using the command line is not your cup of tea, alternate options such as extraction tools are also available that you can go for.


Frequently Asked Questions

How do I unzip a tar GZ file in Linux command line?

You can easily unzip a tar GZ file in Linux by simply entering the code “tar -xf Yourfile.tar.gz” in the Terminal and pressing enter.

Can you unzip a tar GZ file in Windows?

Windows comes with built-in tar-utility and for that, you have to use the command prompt. To unzip a tar gz file in Windows use the following command:
tar -xvzf Yourfile.tar.gz

What type of file is tar?

A tar file is created by the tar command and is used to bundle multiple files and directories into a single file. It does not compress the data; it simply archives it.


Leave a Reply

Your email address will not be published. Required fields are marked *