<img src="https://ws.zoominfo.com/pixel/6169bf9791429100154fc0a2" width="1" height="1" style="display: none;">
Curious about how StrongDM works? 🤔 Learn more here!
Search
Close icon
Search bar icon

How to Extract or Unzip .tar.gz Files in Linux (With Examples)

StrongDM manages and audits access to infrastructure.
  • Role-based, attribute-based, & just-in-time access to infrastructure
  • Connect any person or service to any infrastructure, anywhere
  • Logging like you've never seen

A .tar.gz file is a compressed archive file format that combines the tar and gzip formats. These files are popular among system administrators, developers, and regular computer users for archiving and compression.

You might need to extract or unzip .tar.gz files if you're transferring big datasets or distributing software with Linux, the third-most popular desktop operating system in the world.

This guide tells you everything you need to know about extracting .tar.gz files in Linux, whether you prefer to use the command line or graphical tools. As a result, you can handle these files more efficiently and improve your data management.

Prerequisites

In Linux, you can extract .tar.gz files in two ways:

  • By using the command-line interface, for which you'll need basic knowledge of terminal commands, such as using "ls" and navigating directories
  • With a graphical user interface (GUI)

Extracting .tar.gz files is possible with many different operating systems, including macOS and Windows. However, the instructions below are just for Linux users.

Understanding Tar and Gzip

Before you extract or unzip a .tar.gz file, you should understand its components: tar and gzip.

What Is a Tar Archive?

A tar archive bundles multiple files or directories into a single uncompressed archive, often known as a tarball. This makes it easier for you to distribute software or manage a large number of files. Tar stands for "tape archive."

What Is Gzip Compression?

With gzip, you can compress tar archives to reduce their size. Again, this makes it easier to distribute software or manage multiple files. Gzip compression can also result in more efficient storage.

When gzip compresses a tar archive, it results in a .tar.gz file.

Extracting .tar.gz Files Using the Command Line

The easiest way to extract .tar.gz files is with the Linux command-line interface, also known as the terminal or console. There are three main ways to do this:

  1. Running a basic command
  2. Extracting files to a specific directory
  3. Bulk extraction

Here are the instructions for each method.

1. Basic Command to Extract a .tar.gz File

Start by opening your command-line interface and entering the following command:

tar -xvzf file.tar.gz

This command uses the following flags:

  • -x: extracts files from the archive
  • -v: verbose mode, which shows the extraction process in real time
  • -z: handles gzip compression
  • -f: specifies the filename to extract

2. Extracting Files to a Specific Directory

Extracting files to a location other than your current directory can be a good idea for these reasons:

  • Improving your file organization
  • Preventing the overwriting of files in the directory
  • Increasing space in the directory

To extract files to a new location, use the "-C" option to specify a target directory:

tar -xvzf file.tar.gz -C /path/to/directory

Here's an example:

tar -xvzf archive.tar.gz -C /home/user/Documents/

3. Bulk Extraction of Multiple .tar.gz Files

You can use this method to automate the extraction of several .tar.gz files in a directory. The best way to extract multiple files at the same time is to use a "for" loop:

For file in *.tar.gz; do: tar -xvzf "$file"; done

This is particularly useful for directories with multiple archived files.

All of the above methods require intermediate knowledge of the command line and writing scripts. If you're not comfortable using the command line to extract .tar.gz files, you'll find instructions for completing this task with a GUI later in this guide.

Viewing the Contents Before Extraction

Before extracting a .tar.gz file, you can see what's inside the file without fully extracting the archive.

Here are two approaches you can take.

1. Listing the Archive Contents Without Extracting

To view the contents of a .tar.gz file, run the following command:

tar -tvzf file.tar.gz

This will list the files inside the archive without extracting them.

2. Using Wildcards to List Specific Files

Perhaps you just want to list specific file types, such as .txt files. If this is the case, you can use wildcards.

Here's an example of a command that displays only the .txt files in the archive:

tar -tvzf file.tar.gz --wildcards '*.txt'

Extracting Specific Files or Directories

You might not need to extract everything at once. Follow these instructions if you just want to extract specific files or folders.

1. Extract Only Selected Files

To extract a single file or a specific directory from the archive, specify the file path:

tar -xvzf file.tar.gz path/to/file

For example, to extract "file.txt":

tar -xvzf archive.tar.gz file.txt

2. Use Wildcards to Extract Files

You can also extract specific file types with wildcards.

For example, this command extracts all of the .log files from the archive:

tar -xvzf file.tar.gz --wildcards '*.log'

Extracting Without Overwriting Existing Files

When you're extracting an archive, files with the same name might exist in the directory. Overwriting these files can lead to permanent data loss.

Use the following command to avoid overwriting files:

tar --skip-old-files -xvzf file.tar.gz

This command ensures you extract only new files and don't change existing ones.

Extracting .tar.gz Files From Stdin (Streaming Files)

Downloading and extracting an archive directly from the web in one step can save you lots of time. You can download and extract simultaneously rather than completing these tasks separately.

For this to happen, you need to stream an archive from a URL. You can use the wget command combined with tar:

wget -c https://example.com/file.tar.gz -O - | sudo tar -xz

This command downloads the archive and quickly extracts it without saving the compressed file locally.

Graphical Tools for Extracting .tar.gz Files

You don't have to use the command line to extract .tar.gz files. A GUI can help you handle archives on Linux with fewer commands.

Examples of GUI tools for extracting .tar.gz files include:

Here's how to use these tools for extraction:

  • Open your Linux file manager.
  • Go to the folder that contains the .tar.gz file.
  • Right-click on it and choose "Extract to."
  • Choose your preferred GUI tool and let it extract your files.

Automating Extraction With Bash Scripts

If you have advanced knowledge of the command-line interface, you can automate the extraction process using a simple Bash script. This can be useful for repetitive extraction-based tasks, such as backups.

This command will extract all of the .tar.gz files in a directory:

#!/bin/bash

for file in *.tar.gz; do

 tar -xvzf "$file"

done

This method is especially useful if you regularly work with archives or need to automate the backup extraction process.

Troubleshooting Common Issues

You might come across a few problems when using the command-line interface to extract .tar.gz files. Here are some common errors and how to fix them.

1. Permission-Denied Error

A permission-denied error normally means you don't have sufficient privileges. Use "sudo" to run this command with administrative rights:

sudo tar -xvzf archive.tar.gz

Alternatively, you can use "chmod" to modify file permissions.

If you're struggling with permissions in general, StrongDM can help. Our Zero Trust Privileged Access Management platform can help safeguard your most sensitive data in Linux and other systems.

2. File-Not-Found Error

If you experience this error, make sure the file path is correct. You can use "ls" to list files in the directory and verify the filename. Another tip is to use auto-completion with the "Tab" key to avoid making typos when entering a file path.

3. Corrupted Archive

You can fix a corrupted archive by verifying its integrity with "md5sum" or "sha256sum." Comparing the checksum with the original value will help you confirm whether you downloaded the file correctly.

Bonus Tips: Working With Tar and Gzip

Before you go, here are a couple of extra tips that will help when you're extracting .tar.gz files.

1. Creating a .tar.gz Archive

You can create a .tar.gz archive with the following command:

tar -cvzf archive.tar.gz /path/to/folder

This command bundles files and folders into a compressed archive for easy distribution and storage.

2. Using Gzip Separately

Gzip by itself can help you compress or decompress individual files that don't require the full tar utility. Just use this decompress command:

gzip -d file.tar.gz

Conclusion

Extracting or unzipping .tar.gz files in Linux lets you handle big datasets and distribute software more effectively. While most of the instructions above relate to Linux's command-line interface, you can also use GUI tools to simplify the process. 

Understanding commands is critical for securing and controlling access to the data in your environment. StrongDM streamlines permission management, allowing you to protect your most sensitive systems, including those that handle data archives in Linux.

Book a StrongDM demo now.


About the Author

, Zero Trust Privileged Access Management (PAM), the StrongDM team is building and delivering a Zero Trust Privileged Access Management (PAM), which delivers unparalleled precision in dynamic privileged action control for any type of infrastructure. The frustration-free access stops unsanctioned actions while ensuring continuous compliance.

StrongDM logo
đź’™ this post?
Then get all that StrongDM goodness, right in your inbox.

You May Also Like

How to List Users in Linux (9 Methods with Examples)
How to List Users in Linux (9 Methods with Examples)
Need to keep tabs on who has access to your organization’s Linux system? This guide explores nine methods, with examples, that can help you quickly list users.
SCP Command in Linux: 10 Essential Examples
SCP Command in Linux: 10 Essential Examples
Discover 10 ways to leverage the SCP command in Linux. Learn how to incorporate options for specific file transfers and how to deal with common errors.
How To Use SSH to Connect to a Remote Server (Windows, Linux & Mac)
How To Use SSH to Connect to a Remote Server (Windows, Linux & Mac)
Secure Shell (SSH) is one of the most effective ways to access and manage remote systems. This technology encrypts communications between the client and the server, enhancing system security and preventing unauthorized access. Another important benefit of SSH is its simplicity. This technology is relatively easy to use with various tools and clients, as we will demonstrate below. Plus, you can also use SSH for file transfers, running commands, and even tunneling. This guide explains how to use SSH to connect to remote servers across Windows, Linux, and MacOS environments.
How to Create Users in Linux with useradd (Step-by-Step)
How to Create Users in Linux with useradd (Step-by-Step)
Setting permissions, revoking access, and performing other user management duties in Linux can improve your system's security and organization, ensuring users can access the resources they need when they need to. The useradd command lets you create, modify, and check user accounts, helping you handle multi-user environments across various Linux distributions.
How to Change Password in Linux: A Step-by-Step Guide
How to Change Password in Linux: A Step-by-Step Guide
Explore our in-depth guide on how to change and edit passwords in Linux using various commands and tools. Also, learn some advanced Linux password management techniques.