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

February 12, 2024

Introduction

A .tar.gz file is a compressed archive format commonly used in Linux systems. The format combines multiple files and directories into a single file while reducing their size. It uses tar for archiving and gzip for compression.

Knowing how to unzip a .tar.gz file allows users to extract and access the archives' contents efficiently.

In this guide, you will learn how to extract or unzip .tar.gz files in Linux.

How to Extract or Unzip tar.gz Files in Linux

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to a terminal.

Note: If you are using Windows, check out our guide on how to extract a .tar.gz file in Windows.

How to List Contents of .tar.gz File in Linux

If you don't already have a .tar.gz file, create one with:

tar -czf [archive name] [file(s)/location(s)]

The options -cvf work as follows:

  • c - creates a new archive.
  • z - compresses the file.
  • f - specifies the file name.

For instance, the Home directory contains three files (File1, File2, File3) as confirmed with the ls command:

ls
ls terminal output

Compress the three files in a .tar.gz archive called example1 using the tar command:

tar -czf example1.tar.gz File1.deb File2.deb File3.deb

The command has no output. To verify the change, run ls again:

ls
terminal output for ls

The output shows a newly created archive example1.tar.gz.

Next, list the contents of a .tar.gz file with:

tar -ztvf [archive name]

For instance, list example1.tar.gz contents with:

tar -ztvf example1.tar.gz
tar -ztvf example1.tar.gz terminal output

How to Unzip .tar.gz in Linux via Terminal

Using the terminal to extract .tar.gz files is convenient because users can modify the commands using different options. The following text presents three tools for unzipping .tar.gz archives in Linux.

How to Unzip .tar.gz in Linux using tar

To unzip the .tar.gz file, use the tar command with the following arguments:

tar –xvzf [archive name]

The basic command is tar, followed by four options:

  • x - instructs tar to extract the files from the zipped file.
  • v - lists out the files it's extracting.
  • z - instructs tar to decompress the files.
  • f - tells tar the filename.

Extract .tar.gz in the Current Directory

Unzip the example1 archive in the current directory with the following command:

tar -xvzf example1.tar.gz
tar -xvzf example1.tar.gz terminal output

The command extracts File1, File2, and File3.

Extract Files to a Specific Directory

To extract the files to a specific directory, for example Documents, run:

tar -xvzf example1.tar.gz -C ./Documents
tar xzfC example1.tar.gz ./Documents terminal output

Extract Only Specific Files

To extract only the specific file from the archive, add the file name to the command. For example, extract File1 from example1.tar.gz with:

tar -xvzf example1.tar.gz File1.deb
tar -xvzf example1.tar.gz File1.deb terminal output

Extract Files with a Specific Extension or Name

Use the --wildcards option to extract all files with a certain extension or name. For instance, extract all files with the extension .deb with the following command:

tar -xvzf example1.tar.gz --wildcards '*.deb'
tar -xvzf example1.tar.gz --wildcards '*.deb' terminal output

Extract Files with gzip

gzip is a command-line compression utility used to reduce the size of files or combine multiple files into a single compressed file. However, with the -d option, gzip is able to decompress .tar.gz files. The syntax is:

gzip -d [archive name]

Run the following command to unzip example.tar.gz

gzip -d example.tar.gz
gzip -d example1.tar.gz terminal output

The command shows no output. Run the ls command to verify the outcome:

ls
ls after gzip command

The output shows gzip extracted the example1.tar.gzip file to example1.tar file. To extract files from the .tar archive, run:

tar -xf example1.tar

The command has no output. Verify the changes with ls:

ls
terminal-output for ls after tar xf example1.tar

Unzip Files with gunzip

Another way to unzip a .tar.gzip file is to use gunzip. The gunzip tool is a command opposite to gzip and equivalent to gzip -d. The syntax is:

gunzip [archive name]

To extract files, use gunzip on example1.tar.gz:

gunzip example1.tar.gz
gunzip example1.tar.gz terminal output

The command has no output. Run ls to confirm:

ls
terminal output for the ls command

The command extracts File1.deb, File2.deb, and File3.deb. It also changes example1.tar.gz to example.tar.

Extract Files from a .tar.gz via GUI

A user-friendly way to extract files from a .tar.gz archive is via a Graphical User Interface (GUI). A GUI is more suitable for beginners than a command-line tool.

Extract Files to the Current Directory

Use GUI to unzip the files in the current directory. Take the following steps:

1. Locate the .tar.gz file to unzip. This example uses example1.tar.gz in the Home directory.

2. Right-click the file.

3. Select Extract here.

example1.tar.gz Extract here option

The command extracts files to a new directory called example1, which is located in the current directory.

Extract Files to the Specific Directory

To unzip .tar.gz file and place extracted files in the specific directory, follow these steps:

1. Find the .tar.gz file you want to unzip. In this case, it is example1.tar.gz.

2. Right-click the file.

3. Choose Extract to.

example1.tar.gz extract to option

4. Choose the directory to extract your files to. In this example, it's Documents.

Extract files to the Documents directory

5. Once you choose the directory, click the Select button in the top right corner.

Selecting files in the extract window dialog

The files appear in the directory you selected.

Conclusion

After reading this article, you know how to extract or unzip .tar.gz file in Linux using different methods and tools.

Next, learn two ways to zip a file in Linux.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Unzip a ZIP File in Ubuntu / Linux
December 2, 2019

Zip files are ubiquitously used on all operating system, including Linux. This simple guide explains how to...
Read more
19 Common SSH Commands in Linux With Examples
August 25, 2019

Secure Shell is an important protocol for anyone managing and controlling remote machines. This guide covers...
Read more
How to Set up & Configure ModSecurity on Apache
March 11, 2019

ModSecurity is an Open-source firewall application for Apache. Learn how to Setup & Configure ModSecurity on...
Read more