0% found this document useful (0 votes)
15 views3 pages

How To Extract or Unzip Targz Files in Linux - PhoenixNAP KB

This document provides a comprehensive guide on how to extract or unzip .tar.gz files in Linux, detailing methods using the terminal and GUI. It covers commands for listing contents, extracting files, preserving ownership, and handling specific file extractions. Additionally, it explains the differences between .tar, .tar.gz, and .zip file formats.

Uploaded by

Zac Ing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

How To Extract or Unzip Targz Files in Linux - PhoenixNAP KB

This document provides a comprehensive guide on how to extract or unzip .tar.gz files in Linux, detailing methods using the terminal and GUI. It covers commands for listing contents, extracting files, preserving ownership, and handling specific file extractions. Additionally, it explains the differences between .tar, .tar.gz, and .zip file formats.

Uploaded by

Zac Ing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

https://phoenixnap.

com/kb/extract-tar-gz-files-linux-command-line

Home » KB » SysAdmin » How to Extract or Unzip .tar.gz Files in Linux


How to Extract or Unzip .tar.gz Files in Linux
By Sara Zivanov Published: April 14, 2025
Topics: Linux

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.

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 Copy

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 Copy

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 Copy

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 Copy

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 Copy

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] Copy

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 Copy

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 Copy

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 Copy

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 Copy

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 Copy

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_dirCopy

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 Copy

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 Copy

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
https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line
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' Copy

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 testCopy


dir subdir

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

tar -xvzf example1.tar.gz --wildcards 'subdir/*' Copy

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] Copy

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 Copy

The command shows no output.


2. Run the ls command to verify the outcome:

ls Copy

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 Copy

The command has no output.


4. Verify the changes with ls:

ls Copy

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] Copy

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

gunzip example1.tar.gz Copy

The command has no output. Run ls to confirm:

ls Copy

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 Copy

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 Copy

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

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

The command has no output.


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

ls Copy

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


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

gunzip file.txt.gz Copy

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 Copy

6. To verify the outcome, run ls again:

ls Copy

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.

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.

4. Choose the directory to extract your files to. In this example, it's Documents.
https://phoenixnap.com/kb/extract-tar-gz-files-linux-command-line

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

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.

You might also like