How to Extract .tar.gz Files in Linux

A .tar.gz
file is an archive or compressed file created with the Gzip compression algorithm. Tar stands for "Tape Archive." This format bundles multiple files and folders into a single archive file. It supports multiple compression algorithms, such as .xz
, .bzip2
, and .gz
.
The .tar
compression typically packages files and folders into one file. It's widely used to package and distribute software and file backups. When compiling software from source code, you will most likely download a .tar.gz
, file, which ships with the source code, libraries, frameworks, and everything you need to install an application
Let’s dive in and explore various ways of extracting a .tar.gz
archive.
#How to extract a tar.gz archive
There are different approaches to extracting a .tar.gz
archive. The tar
command offers flexible options for unpacking the archive’s contents. By default, the tar command ships with most Linux distributions. Hence, no installation is required.
Deploy and scale your projects with Cherry Servers' cost-effective dedicated or virtual servers. Enjoy seamless scaling, pay-as-you-go pricing, and 24/7 expert support—all within a hassle-free cloud environment.
#Syntax
To extract a tar.gz file, use the following syntax:
$ sudo tar -xvf file.tar.gz
Let's explore the command options:
The -x
option extracts the file contents to a folder bearing the archive name
The -v
option is for verbose output. It displays the names of files being extracted to stdout.
The -f
option specifies the archive's file name.
The tar command autodetects the compression type and extracts the archive’s contents. The same syntax can extract compressed archives with algorithms such as .tar.xz
, .tar.bzip2
, and .tar.bz2
.
To demonstrate this, download Ruby’s source code, which is packaged as a tar archive using the following command:
wget https://cache.ruby-lang.org/pub/ruby/3.4/ruby-3.4.1.tar.gz
Run the ls -l
command to confirm the file’s existence.
To extract the file, run the command:
sudo tar -xvf ruby-3.4.1.tar.gz
The command extracts the archive’s contents to a directory named ruby-3.4.1
in the current working directory.
#Extract a tar.gz archive to a different path
Typically, the tar command extracts an archive to your present working directory. To alter this behavior and extract it to a different path, use the -C
option followed by the desired directory path.
Here’s the syntax for context.
sudo tar -xvf file.tar.gz -C /path/to/destination/folder
Let’s see this in action.
Create a new directory called project_dir
in your home directory.
mkdir project_dir
Since it is an empty directory, it should be about 4Kb in size. Use the du
command to confirm this.
du -sh project_dir
Next, extract the Ruby archive to the project_dir
as follows. Be sure to replace username
with your username.
tar -xf ruby-3.4.1.tar.gz -C /home/username/project_dir
Once again, check the disk usage of the project_dir
directory. This time, it’s 156M.
du -sh project_dir
List the directory’s contents and you will notice a new directory created when the archive was extracted.
ls project_dir
#Extract specific files from a tar.gz archive
Sometimes, you may want to extract a few select files from an archive and not all. To extract specific files from a tar.gz archive, simply include the file names after the archive name as shown:
tar -xf file.tar.gz file1 file2
We will create an archive from scratch from a few text files to demonstrate this. So follow the steps below.
First, create five text files using the touch
command as shown.
touch file{1..5}.txt
Next, compress all the files into an archive called archive.tar.gz
. The -c
option compresses the files, while the *.txt
wildcard notation denotes all the files with a .txt
file extension.
tar -cvf archive.tar.gz *.txt
Up to this point, we have a .tar.gz
archive and the original text files. Since we have already compressed the text files into an archive, you can safely delete them as shown.
rm *.txt
To extract, the file1.txt
and file2.txt
files from the archive, run the command:
tar -xvf archive.tar.gz file1.txt file2.txt
Follow the same script when extracting directories from an archive. For example, to extract directory1
and directory2
directories only from an archive, run:
tar -xvf archive.tar.gz directory1 directory2
#Extract tar.gz files using a wildcard pattern
In addition to extracting individual files from an archive, you can extract files based on a wildcard pattern by specifying the --wildcards
flag followed by the pattern in single quotes.
Here’s an example of extracting all the text files from an archive.
tar -xvf archive.tar.gz --wildcards '*.txt'
#List files in a tar.gz archive
Sometimes, all you want is to list an archive’s contents without extracting them. The -t
option allows you to do exactly that. For example, to list the files in the archive.tar.gz
file, run the command:
tar -tf archive.tar.gz
#Conclusion
This tutorial has demonstrated various ways of extracting a .tar.gz
archive. You have seen how to extract an entire tar archive, extract individual files, and use wildcard matching to extract files with the same file extension. Lastly, we have shown you how to list an archive’s contents in case you need to have a glance. Hopefully, you are equipped with the skills required to extract an archive.
Cloud VPS Hosting
Starting at just $3.24 / month, get virtual servers with top-tier performance.