Spring Sale - up to 36% OFF

How to Extract .tar.gz Files in Linux

How to Extract .tar.gz Files in Linux
Published on May 12, 2025 Updated on May 12, 2025

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.

tar-archive-file

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.

tar-archive-extracted-to-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-a-tar-archive-to-a-different-directory

#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

compress-files-into-a-tar-archive

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

extract-a-few-files-from-tar-archive

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'

extract--files-from-archive-using-wildcard-pattern

#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

list-files-from-an-archive

#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.

Share this article

Related Articles

Published on Jun 7, 2021 Updated on Jun 29, 2022

AlmaLinux Review: a CentOS Clone Supported by CloudLinux

AlmaLinux is an open-source Linux distribution focused on long-term stability, that is a 1:1 binary compatible fork of Red Hat Enterprise Linux (RHEL)

Read More
Published on May 31, 2022 Updated on May 5, 2023

A Complete Guide to Linux Bash History

Learn how to work with Bash history to become more efficient with any modern *nix operating system.

Read More
Published on Jan 26, 2022 Updated on Jun 15, 2023

How to Use Cron to Automate Linux Jobs on Ubuntu 20.04

Learn how to use Cron - the most popular Linux workload automation tool that is widely used in Linux community - to automate Linux jobs on Ubuntu 20.04.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 42490a85e.1137