How To Extract or Unzip Targz Files in Linux - PhoenixNAP KB
How To Extract or Unzip Targz Files in Linux - PhoenixNAP KB
com/kb/extract-tar-gz-files-linux-command-line
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.
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:
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
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.
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:
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.
To extract multiple specific files, separate each file name with a space. For instance, extract File1 and File2 from
example1.tar.gz with:
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:
2. Extract just the file FileA.txt by using the full path shown in the previous step:
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:
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:
The command doesn't produce an output. Next, extract all files from subdir with:
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:
ls Copy
ls 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:
ls Copy
The command has no output. This restores the original file.txt in the same directory. The same result is achieved using:
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.
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.
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.