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.
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
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
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
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
- instructstar
to extract the files from the zipped file.v
- lists out the files it's extracting.z
- instructstar
to decompress the files.f
- tellstar
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
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
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
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'
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
The command shows no output. Run the ls
command to verify the outcome:
ls
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
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
The command has no output. Run ls
to confirm:
ls
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.
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.
4. Choose the directory to extract your files to. In this example, it's Documents.
5. Once you choose the directory, click the Select button in the top right corner.
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.