Categories: TutorialsUbuntu

How to Extract/Unzip tar.gz Files from Linux/Ubuntu Commands?

Software packages are available to download in the form of compressed archives from the software developer’s website. These archives are extracted before the installation process. There are many compressed archives like .deb, .rpm, tar.gz, etc. among which, tar.gz is widely used for software distribution. The compression and extraction of tar.gz archives is supported by the command line tool tar in Linux/Ubuntu.

This article will discuss different scenarios of extracting the tar.gz archive on Linux/Ubuntu 22.04 LTS systems.

How to Extract/Unzip tar.gz Files from Linux/Ubuntu Commands?

The “tar.gz” is a compressed archive file format for Linux systems that uses the “gzip” compression algorithm. “.tar.gz” archives can be created by How to Create a Tar File in Linux/Ubuntu?

Sponsored
Consider that we have an archive “archive.tar.gz” in the “SampleDirector1” directory. In the following sections, three scenarios of the extraction files from archive.tar.gz will be discussed:
  • Scenario 1: Extract/Unzip “.tar.gz” Files in Current Working Directory.
  • Scenario 2: Extract/Unzip “.tar.gz” Files to a Specific Directory.
  • Scenario 3: Extract/Unzip Specific Files from a “.tar.gz” Archive.

Scenario 1: Extract/Unzip “.tar.gz” Files in the Current Working Directory

The archive “Archive.tar.gz” can be extracted in the same directory by the following command:

$ tar xzf archive.tar.gz

The above command comprises of:

  • tar: tar command line tool
  • x: extract files from an archive.
  • z: constructs the compressed/extracted archive using “gzip”.
  • f: Read the archive -archive.tar.gz (name of the archive file)

We can verify the extraction of the “archive.tar.gz” archive by:

The above image shows that archive.tar.gz is successfully extracted to the “SampleDirectory1” directory.

Scenario 2: Extract/Unzip “.tar.gz” Files to a Specific Directory

If we would like to extract the “archive.tar.gz” archive in a specific directory, i.e., in the “Others” directory inside the “Downloads” directory, we can run the following command:

$ tar xzf archive.tar.gz -C /home/linuxuser/Downloads/Others/

The above command comprises of:

  • -C: Extract the “tar.gz” files in a specific directory.
  • /home/linuxuser/Downloads/Others/: path where the files will be extracted.

Sponsored

We can verify the extraction of the “archive.tar.gz” archive by navigating to the specified path:

$ cd /home/linuxuser/Downloads/Others/

$ ls -l

The above image shows that archive.tar.gz is successfully extracted to the “Others” directory (home/linuxuser/Downloads/Others) from the “SampleDirector1” directory.

Scenario 3: Extract/Unzip Specific Files from a “.tar.gz” Archive

There can be situations where all the files compressed in the archive are not required and only a file or few files are required. “Tar” allows the extraction of a file or a few files. The following command will extract only File3.txt and File5.txt from archive.tar.gz:

$ tar xzvf archive.tar.gz File3.txt File5.txt

In the above command, the v flag indicates verbose output.

We can verify the extraction of specific files of the “archive.tar.gz” archive by:

$ ls -l

The above image shows that only File3.txt and File5.txt are successfully extracted to the “SampleDirectory1” directory.

Conclusion

Tar.gz archives are extracted using the tar command line tool in either the current working directory or in a specific directory by using the “tar xzf ” and “tar xzf -C ” commands respectively. Additionally, specific files can be extracted from the archive by running “tar xzvf ”. This article discussed different scenarios of extracting the tar.gz archive on Linux/Ubuntu 22.04 LTS systems.

Ubuntu Server Admin

Recent Posts

From sales development to renewals: Mariam Tawakol’s career progression at Canonical

Career progression doesn’t follow a single path – and at Canonical, we embrace that. Our…

9 hours ago

In pursuit of quality: UX for documentation authors

Canonical’s Platform Engineering team has been hard at work crafting documentation in Rockcraft and Charmcraft…

9 hours ago

Ubuntu 24.10 (Oracular Oriole) reached End of Life on 10th July 2025

This is a follow-up to the End of Life warning sent earlier to confirm that…

1 day ago

Canonical announces Charmed Feast: A production-grade feature store for your open source MLOps stack

July 10, 2025: Today, Canonical announced the release of Charmed Feast, an enterprise solution for…

1 day ago

Ubuntu Adding NVIDIA 575 Driver Support for 24.04 & 22.04 LTS

For NVIDIA users, the latest 575 series driver will be available soon in Ubuntu repository.…

2 days ago

Amarok Released 3.3 with Qt6/KF6 Port & GStreamer Backend

Amarok, the free open-source KDE music player, released new 3.3 version on Tuesday, named “Far…

2 days ago