Learning Center
Linux

How to Extract or Unzip .tar.gz Files in Linux

28 Jan 2025
Awais Khan
Awais Khan

Exploring the Linux landscape often means dealing with several file formats, especially compressed ones like .tar.gz. This format is popular because it combines multiple documents and folders into one compressed archive. Whether you're obtaining software packages, organizing project backups, or overseeing data storage, mastering this format usage is essential. 

Throughout this guide, we will examine various strategies for unpacking .gz archives in Linux. From the versatile tar command to the more straightforward gzip and gunzip commands, we'll cover everything. We'll also dive into combining commands like unzip and tar, and using graphical interfaces for those who prefer a more visual approach.

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

Why Choose .tar.gz?
Copy link

Listed below are few key reasons why you might opt to utilize this format:

  • Space Efficiency: The combination of tar and gzip allows for the streamlined compression of large data amounts, enhancing disk space usage.

  • Simplified Data Management: Merging several documents and directories into a single archive enhances data management and organizes storage.

  • Easy Distribution: This widely-adopted format ensures seamless transfers between systems without any compatibility hurdles.

  • Preservation of Metadata: The tar utility maintains file permissions and timestamps, making it perfect for backups and migrating systems.

Creating a .tar.gz File
Copy link

Before jumping into extraction, it's helpful to know how to create an archive. This makes it easier to combine and compress many documents into one neat, smaller package.

Here is the standard syntax for creation:

tar -czf archive-name.tar.gz file1 file2 directory1

Where:

  • c: Creates an entirely new archive.
  • z: Perform compression.
  • f: Assigns a specific name to the archive.

For instance, to compress report1, report2, and the directory projects into a file called backup, apply:

tar -czf backup.tar.gz report1.txt report2.txt projects

Image1

For verification, list the directory items via:

ls

Image3

Examining .tar.gz Content
Copy link

To examine the items without extracting them, use a command that lists every compressed item. This is particularly handy for verifying items before unpacking.

To list .gz content:

tar -ztvf archive-name.tar.gz

For instance, to list the items of backup:

tar -ztvf backup.tar.gz

Image2

Extracting .tar.gz in Linux
Copy link

Linux offers a variety of extraction methods for these archives, each bringing its own advantages. Here are comprehensive instructions for utilizing various commands and tools.

Method 1: Via tar Utility
Copy link

The tar command is a powerful and flexible utility designed to manage compressed documents, offering functions to create, extract, and display the items of archives. This command is your ultimate tool for handling .gz resources efficiently.

Basic Extraction

To unpack .gz items directly into the current directory, apply:

tar -xvzf archive-name.tar.gz

Where:

  • x: Unpacks the archive's items.
  • v: Verbose mode actively displays each file being unpacked.
  • z: Decompresses the data.
  • f: Gives the archive a unique name.

For unpacking the backup, apply:

tar -xvzf backup.tar.gz

Image5

Extracting to a Specific Directory

For placing the unpacked files in a different location, use the -C option to indicate your chosen directory. This is handy when you need to ensure your retrieved file are neatly arranged in a designated location.

To unpack the items into a chosen directory, apply:

tar -xvzf archive-name.tar.gz -C /path/to/destination

For instance, to unpack the backup into the Documents folder, utilize:

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

Image4

Extracting Specific Content

For retrieving certain items from the archive, simply provide their names. This enables you to pinpoint and retrieve just the necessary data. 

Here’s the format:

tar -xvzf archive-name.tar.gz file1 file2

For example, to retrieve report1 and report2 from backup, apply:

tar -xvzf backup.tar.gz report1.txt report2.txt

Image7

Extracting Contents with a Specific Extension

For retrieving items with a particular extension, the --wildcards option proves to be quite useful. This option lets you filter and retrieve data based on their names or extensions.

Here's the syntax:

tar -xvzf archive-name.tar.gz --wildcards '*.txt'

For instance, to retrieve all .txt docs from backup:

tar -xvzf backup.tar.gz --wildcards '*.txt'

Image6

Method 2: Via gzip Utility
Copy link

The gzip is a tool primarily used for compressing data, but it can also decompress them with the -d option. This method is straightforward and effective for handling .gz resources.

To unzip a .gz file, apply the subsequent command:

gzip -d archive-name.tar.gz

For instance, to unpack backup, apply:

gzip -d backup.tar.gz

Image9

After decompressing, retrieve the items via:

tar -xf archive-name.tar

For instance:

tar -xf backup.tar

Image8

Method 3: Via gunzip Utility
Copy link

The gunzip is a specifically designed tool for decompressing .gz documents, functioning as an alias for gzip -d. This command is simple to use and directly addresses the need to decompress .gz files.

To decompress, apply:

gunzip archive-name.tar.gz

For example:

gunzip backup.tar.gz

Image11

After decompressing, unpack the items through:

tar -xf archive-name.tar

For example:

tar -xf backup.tar

Image8

Method 4: Via GUI
Copy link

For users who favor a GUI, various Linux desktop environments include file managers equipped with extraction tools. This method is user-friendly and ideal for beginners.

Extracting Contents to the Current Directory

  1. Find the .gz file within your file manager.

Image10

  1. Right-click on it and choose "Extract."

Image13

Extracting Contents to a Specific Directory

  1. Spot the .gz file within your file explorer.

  2. Right-click on it and select "Extract to…".

Image14

  1. Choose the destination directory.

Image15

Handling Large Archives with Parallel Decompression
Copy link

When handling massive archives, pigz (Parallel Implementation of gzip) can significantly enhance decompression speed by using several CPU cores. Here's how to use it:

Install pigz on Linux via:

sudo apt install pigz

To uncompress a .gz file via pigz, apply:

pigz -d archive-name.tar.gz

After decompression, retrieve the resulting .tar doc with:

tar -xf archive-name.tar

Utilizing Compression with Encryption
Copy link

For added security, you can encrypt your .gz doc. GPG (GNU Privacy Guard) can be used to encrypt documents, ensuring that sensitive information remains protected during storage and transfer.

Encrypting an Archive

For encryption, use GPG with the following command:

gpg -c archive-name.tar.gz

Decrypting an Archive

To decrypt an encrypted archive, apply:

gpg -d archive-name.tar.gz.gpg > archive-name.tar.gz

Tips for Content Extraction in Linux
Copy link

  • Backup Important Docs: Always create backups before unpacking multiple docs to avoid data loss.
  • Check Permissions: Ensure you possess the required permissions to retrieve documents in the designated directory.
  • Utilize Wildcards Carefully: Be cautious when using wildcards to avoid unintentional extraction.

Troubleshooting Frequent Issues with Extraction
Copy link

Here are a few common extraction difficulties and the ways to address them:

Corrupted Archives

In case an archive is corrupted, try using the --ignore-zeros option to retrieve it:

tar -xvzf archive-name.tar.gz --ignore-zeros

Insufficient Permissions

Confirm that you have the proper permissions to access and modify files. Utilize sudo if required:

sudo tar -xvzf archive-name.tar.gz -C /path/to/destination

Disk Space Issues

Check that you have enough disk space to unzip the documents. Verify disk usage with:

df -h

Conclusion
Copy link

Unpacking .tar.gz documents in Linux is a simple task, with multiple methods to cater to different user preferences. Whether you're using the tar, gzip, gunzip commands, or a GUI, Linux equips you with efficient tools to handle compressed data seamlessly. This guide empowers you with the know-how to confidently retrieve .gz docs. Whether it's handling software packages, arranging backups, or managing data storage, mastering the creation and extraction of such files keeps your workflow streamlined and efficient. 

By mastering the creation and extraction of these files, you streamline your workflow and enhance your overall efficiency, making data management a breeze.