Extracting tar.gz File in Linux: Step-by-Step Guide

Have you come across a .tar.gz file in Linux and wondered, “What’s this, and how do I open it?” Don’t worry – this guide will show you on extracting tar.gz file in Linux step by step, in the simplest way possible. Whether you’re new to Linux or just need a quick refresher, we’ve got you covered.

What Is a tar.gz File?

Imagine that you have a box of books (files and folders) that you want to share with a friend. So, instead of handing them each book individually, you pack them all into one box. That’s what a .tar file does, it groups multiple files and folders into one single file.

Now, imagine that box is really big and bulky. To make it easier to carry, you shrink it down to half its size. That’s what .gz does – it compresses the .tar file to save space.

So, a .tar.gz file is like a packed and compressed box of files.

Why Do We Use tar.gz Files?

    • Saves Space: Compressing files reduces the size.
    • Easier Sharing: Collecting multiple files into one makes transferring easier.
    • Common in Linux: Many programs and datasets are shared in .tar.gz format.

Extracting tar.gz File in Linux

Here’s how you can extract a .tar.gz file using the terminal. Don’t worry, it’s as simple as typing a few commands.

→ Basic Command to Extract Files

Use this command to extract a .tar.gz file:

				
					tar -xvzf filename.tar.gz
				
			

→ Steps:

    1. Open the Terminal: Look for “Terminal” in your apps and open it.
    2. Go to the File’s Location: If your file is in the Downloads folder, type:
				
					cd ~/Downloads
				
			
    1. Run the Extraction Command: Replace filename.tar.gz with your file’s name and type:
				
					tar -xvzf filename.tar.gz
				
			

After running this command, the files will be extracted to the same folder where the .tar.gz file is located.

Extracting tar.gz File to a Specific Folder

Want to keep your extracted files organized? You an follow the steps to extract it directly to a folder.

    1. Create a Folder:
				
					mkdir extracted-files
				
			
    1. Extract the File into the New Folder:
				
					tar -xvzf filename.tar.gz -C extracted-files/
				
			

This will place all the extracted files inside the extracted-files folder.

Checking What’s Inside a tar.gz File Before Extracting

If you are not sure what’s in the file? You can check its contents without extracting them. Here is what you can do:

				
					tar -tvzf filename.tar.gz
				
			

This command will list all the files and folders inside the .tar.gz file. It’s like peeking inside the box before opening it.

Handling Errors While Extracting

Below are some common issues that you might face and here is how you can fix them:

    • Permission Denied: If you see this error, run the command with sudo (administrator access):
				
					sudo tar -xvzf filename.tar.gz
				
			
    • File Not Found: Double-check the file’s name and location. If it’s in another folder, provide the full path:
				
					tar -xvzf /home/user/Downloads/filename.tar.gz
				
			

Using the GUI to Extract tar.gz Files

Getting confused on how to use the terminal and not getting to how to handle it? You can extract .tar.gz files using the file manager:

    1. Find the .tar.gz file in your file explorer.
    2. Right-Click on the file.
    3. Select Extract Here to extract the files into the current folder.
    4. Or choose Extract To… if you want to specify a different folder.

Real-Life Example

Let’s consider an example, you have downloaded a file called data.tar.gz, and you want to extract it into a folder named data-folder inside your Documents.

Follow the below steps to achieve it:

    • Open the terminal.
    • Navigate to the Documents folder:
				
					cd ~/Documents
				
			
    • Create the data-folder:
				
					mkdir data-folder
				
			
    • Extract the file into the folder:
				
					tar -xvzf ~/Downloads/data.tar.gz -C data-folder/
				
			

Summary

Extracting tar.gz file in Linux is straightforward. Whether you prefer the terminal or the graphical interface, you can easily extract these files in just a few steps. Here’s a quick recap:

    1. Use tar -xvzf to extract files.
    2. Use -C to specify a folder.
    3. Use -t to view the contents before extracting.
    4. Handle errors with sudo or full file paths.

References

Scroll to Top