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

Introduction

A .tar.gz file is a commonly used compressed archive format in Linux environments. It combines multiple files and directories into a single file, reducing their overall size. This format utilizes tar for archiving and gzip for compression.

Knowing how to unzip a .tar.gz file is essential as it allows users to efficiently extract and access the contents of the archives.

This guide will teach you how to extract or unzip .tar.gz files in Linux.

Prerequisites:

  • A Linux system
  • Access to a terminal

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

If you don’t have a .tar.gz file yet, you can create one with the following command:

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

The options -cvf function in the following manner:

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

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

ls
ls terminal output

Use the tar command to compress the three files into a .tar.gz archive named example1:

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

The command produces no output. To confirm the change, run ls again:

ls
terminal output for ls

The output displays a newly created archive named example1.tar.gz.

To list the contents of a .tar.gz file, use the command:

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

Leveraging the terminal for extracting .tar.gz files is advantageous as users can customize commands using various options. Below, we introduce three methods 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 fundamental command consists of 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: Specifies the filename.

To extract .tar.gz in the current directory, unzip the example1 archive with the following command:

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

The command extracts File1File2, and File3.

Extract Files to a Specific Directory

To extract the files to a specific directory, like Documents, use the following command:

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

Extract Only Specific Files

To extract only a specific file from the archive, include the filename in the command. For instance, to extract File1 from example1.tar.gz, use the following command:

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

Extract Files with a Specific Extension or Name

Utilize the –wildcards option to extract all files with a specific extension or name. For example, to extract all files with the extension .deb, use 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 versatile command-line compression tool mainly used to reduce file sizes or combine multiple files into one compressed archive. Surprisingly, it can also unpack .tar.gz files when the -d option is used. Here’s the syntax for decompression:

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 indicates that gzip extracted the example1.tar.gzip file to example1.tar. To extract files from the .tar archive, execute:

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 method to unzip a .tar.gzip file is by utilizing gunzip. The gunzip tool serves as the opposite command to gzip and is 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. Additionally, it renames example1.tar.gz to example.tar.

Extract Files from a .tar.gz via GUI

For a more user-friendly approach to extracting files from a .tar.gz archive, consider using a Graphical User Interface (GUI). GUIs are typically more accessible for beginners compared to command-line tools.

Extract Files to the Current Directory

Utilize the GUI to unzip the files in the current directory by following these steps:

  1. Navigate to the .tar.gz file you want to unzip. In this example, we’ll use example1.tar.gz located in the Home directory.
  2. Right-click on the file.
  3. Choose “Extract here” from the context menu.
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 a .tar.gz file and extract the files into a specific directory, follow these steps:

  1. Locate the .tar.gz file you wish to unzip. In this instance, it’s example1.tar.gz.
  2. Right-click on the file.
  3. Select “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

Upon reading this article, you’ll gain an understanding of several methods and tools for extracting or unzipping .tar.gz files in Linux.

Leave a Comment