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

By
Sara Zivanov
Published:
April 14, 2025
Topics:

A .tar.gz file is a compressed archive format 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 archive's contents efficiently.

In this guide, you will learn how to extract or unzip .tar.gz files in Linux. The tutorial also explains how to handle specific file extractions, preserve file ownership, and ensure security when working with untrusted archives.

How to Extract or Unzip tar.gz Files in Linux

Prerequisites

  • A Linux system.
  • 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

Before extracting files, check the contents of a.tar.gz file. This is an important security step if you didn't create the file yourself. List the archive contents to verify file names so you don't accidentally overwrite system files or move contents where they don't belong.

To show how this works, create an archive and list its contents. Take the following steps:

1. Run the ls command to display the contents of the current directory. In this example, it includes three .deb files:

ls
ls terminal output

The Home directory contains three files (File1, File2, File3 - colored in red) as confirmed with the ls command.

If the files aren't already there, create them using the echo command. For example:

echo "This is File1" > File1.deb<br>echo "This is File2" > File2.deb<br>echo "This is File3" > File3.deb

These commands create text files named File1.deb, File2.deb, and File3.deb in the current directory.

2. Use tar with the -czf options to combine and compress the files into a single archive called example1.tar.gz:

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

The command has no output. However, the options -czf work as follows:

  • c. Creates a new archive.
  • z. Compresses the archive.
  • f. Specifies the archive file name.

3. Run ls again to confirm example1.tar.gz has been created:

ls
Terminal output for ls

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

4. List the contents of a .tar.gz file with:

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

The -t option in this command lists the archive contents.

Note: Always list the contents of untrusted archives before extracting them. This helps detect suspicious paths like ../ that could attempt to write files outside the intended extraction directory. Use tar -ztvf to inspect the archive and confirm its contents safely.

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 with different options. The following text presents three tools for unzipping .tar.gz archives in Linux.

Extract .tar.gz Files via 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.

The following sections elaborate on concrete examples.

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 -xvzf example1.tar.gz -C ./Documents terminal output

If you're extracting as the root user and want to preserve original file ownership, use the --same-owner option. Run the command with sudo, or use su to switch to root and run it directly:

sudo tar -xvzf example1.tar.gz -C ./Documents --same-owner
sudo tar -xvzf example1.tar.gz -C ./Documents --same-owner terminal output

Using the --same-owner flag ensures the extracted files retain the original ownership (i.e., the user who created the archive), even when running as root.

Note: If any of the extracted files already exists in the target directory, tar overwrites them without prompting. Use caution to avoid accidental file replacement.

Extract Only Specific Files

To extract only a single file from a .tar.gz archive, provide its exact name, as listed in the archive. 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

To extract multiple specific files, separate each file name with a space. For instance, extract File1 and File2 from example1.tar.gz with:

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

Extract Files from an Archive with Subdirectories

Archives can include directory structures. Therefore, when extracting from a .tar.gz archive that contains such a structure, you must specify the full internal path to the file, exactly as stored in the archive.

In previous examples, the archive included files in the root, so only the filenames were needed. However, if an archive was created with files stored in subdirectories, use the exact path shown inside the archive.

To see how this works, first create a directory structure with files using mkdir and touch commands, and then use tar to archive them:

mkdir -p example2_dir/subdir && touch example2_dir/subdir/FileA.txt<br>tar -czf example2.tar.gz example2_dir

The command has no output but creates a compressed archive named example2.tar.gz, which contains a directory example2_dir with a subdirectory subdir that includes one file named FileA.txt.

Take the following steps to extract these files:

1. View the internal structure and file paths, as you will need them later:

tar -ztvf example2.tar.gz
tar -ztvf example2.tar.gz terminal output

2. Extract just the file FileA.txt by using the full path shown in the previous step:

tar -xvzf example2.tar.gz example2_dir/subdir/FileA.txt
tar -xvzf example2.tar.gz example2_dir/subdir/FileA.txt terminal output

This command extracts only FileA.txt from inside the subdir directory, leaving the rest of the archive untouched.

Extract Files with a Specific Extension or Name

To extract files with a specific pattern or extension, use the --wildcards option. This allows you to target files that match a certain pattern.

For instance, to extract all files with the .deb extension from the archive, use the following command:

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

The *.deb wildcard pattern matches any file that ends with .deb. Ensure you enclose the wildcard pattern in single quotes to prevent the shell from expanding before the tar command processes it.

However, if the archive contains a directory and you want to extract all files from it, specify the directory name followed by /*.

For example, to extract all files from a directory called subdir, first create the directory structure and add some files using mkdir and touch commands:

mkdir -p testdir/subdir && touch testdir/subdir/File1.txt testdir/subdir/File2.txt<br>tar -czf example1.tar.gz -C testdir subdir

The command doesn't produce an output. Next, extract all files from subdir with:

tar -xvzf example1.tar.gz --wildcards 'subdir/*'
tar -xvzf example1.tar.gz --wildcards 'subdir/*' terminal output

Extract .tar.gz Files via 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]

Take the following steps to extract .tar.gz this way:

1. Run the following command to unzip example1.tar.gz

gzip -d example1.tar.gz

The command shows no output.

2. Run the ls command to verify the outcome:

ls
Terminal output for ls command

The output shows gzip extracted the example1.tar.gzip file to example1.tar archive.

3. To extract files from the .tar archive, run:

tar -xf example1.tar

The command has no output.

4. Verify the changes with ls:

ls
ls command terminal output

The command extracted the .deb files (File1.deb, File2.deb, File3.deb).

Extract .tar.gz Files via 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

The command has no output. Run ls to confirm:

ls
the ls command terminal output

The command extracts File1.deb, File2.deb, and File3.deb.

Unlike gzip -d, the gunzip command removes the original .tar.gz file after decompressing it, leaving only the .tar archive behind.

Decompress Standalone .gz Files

Some .gz files are not archives. Moreover, they contain a single compressed file. To decompress a standalone file, take the following steps:

1. Create a text file and compress it into a .gz file using the echo command:

echo "This is a test file." > file.txt<br>gzip file.txt
echo "This is a test file." > file.txtgzip file.txt

The echo command writes the text This is a test file. into a new file called file.txt. This file is then compressed into file.txt.gz using gzip.

2. Run ls to confirm file.txt.gz has been created:

ls
echo "This is a test file." > file.txtgzip file.txt terminal output

3. Extract the contents with gzip -dc while keeping the original .gz file:

gzip -dc file.txt.gz > file.txt

The command has no output.

4. Run ls to confirm both files are now present:

ls
Terminal output ls

The output shows both file.txt.gz and file.txt.

5. Use gunzip to extract and remove the .gz file:

gunzip file.txt.gz

The command has no output. This restores the original file.txt in the same directory. The same result is achieved using:

gzip -d file.txt.gz

6. To verify the outcome, run ls again:

ls
Terminal output ls command

The output shows only file.txt., which means the gunzip command extracted the contents of file.txt.gz and deleted the original .gz file.

How to Unzip .tar.gz in Linux Using the 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 .tar.gz Files to 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.

GUI Extract option

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

Extract .tar.gz Files to 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.

GUI Extract to option

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

Choose documents

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

Click the Select button

The files appear in the directory you selected.

FAQ

This section addresses common questions about .tar, .tar.gz, and .zip files, their purposes, and how they differ from each other.

What Are .tar and .tar.gz Files?

A .tar file is an archive format that consolidates multiple files into one without compressing them. In contrast, a .tar.gz file is a .tar archive that has been compressed using the gzip algorithm, making it smaller and more efficient for storage or transfer.

By combining the two, you get both an archive and a compressed file in one, which is especially useful in Unix-based systems for packaging and distributing large collections of files.

tar vs. tar.gz

While a .tar file serves only to archive files, a .tar.gz file combines archiving and compression, helping to reduce file size. gzip compresses the archive, creating a .tar.gz file that's easier to store and transfer.

On Unix and Linux systems, the tar command is often used to group files together, and gzip is applied to compress the entire archive into a single file, saving both space and bandwidth.

tar vs .zip

The main difference between .tar and .zip is that they handle compression differently.

A .tar file archives multiple files without compression, whereas a .zip file both archives and compresses them at once.

While .tar files are mainly used on Unix-like systems, .zip is a cross-platform format widely supported across operating systems like Windows, macOS, and Linux.

One advantage of .zip is its ability to compress files individually, unlike .tar.gz files, where compression is applied to the whole archive. Additionally, .tar files preserve metadata, such as permissions, which makes them ideal for backup and transfer in Unix systems.

Conclusion

This article elaborated on how to list contents and extract or unzip .tar.gz files in Linux using different methods and tools, either via the terminal or GUI. It also explained the key differences between .tar, .tar.gz, and .zip files.

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

Was this article helpful?
YesNo