How to extract .gz and .tar.gz files in Linux

Andrei Maksimov
Andrei Maksimov

When transferring files over the network, it’s preferable to transfer a single file (usually .gz or .tar.gz archive). Such an approach allows for minimizing disk IO operations and speeding up the file transmission process. A single file of 1GiB in size will be transferred faster than 1024 files of 1MiB in size. In this article, we’ll look at the process of extracting and .gz and .tar.gz files in Linux.

There are two most commonly used utilities for extracting and opening file archives in Linux:

  • gzip
  • tar

‘gzip’ command

gzip The most commonly used tool in the Linux world reduces file size using Lempel-Ziv coding (LZ77) while keeping the original file mode, timestamp, and ownership.

By the way, the same algorithm is used for compressing web elements which allow loading web pages faster.

Usually, gzip-compressed file ends with a .z or .gz file extension.

As an example, let’s download an archive of WordPress, the most popular CMS:

wget https://wordpress.org/latest.tar.gz
1. How to Open and Extract .gz files in Linux - Download WordPress

Now, you can extract it:

gzip -d latest.tar.gz
3. How to Open and Extract .gz files in Linux - gzip

You’ll achieve the same result if you use gunzip command which is an alias for gzip -d command:

2. How to Open and Extract .gz files in Linux - gunzip

The result in both cases will be the same, the tar archive.

But, wait, why do we need two archives?!

Difference between ‘gzip’ and ‘tar’

gzip is an archival utility responsible for the file’s compression, but it does not support multiple files. Initially, it was designed to compress only one file at a time.

tar is an archival utility meaning that it is responsible for putting multiple files into a single file which is called an “archive” too.

At the beginning of the Unix world, tar archives were used to store files on magnetic tapes. The name “tar” comes from this use; it stands for tape archiver.

That’s why we need tar.

‘tar’ command

The tar utility initially was responsible for putting multiple files into a single location (a magnetic type, which was the only backup storage available). Nowadays, when the storage is cheap and available, tar is used to put the files into a single file.

Let’s get WordPress files:

tar xf latest.tar
4. How to Open and Extract .gz files in Linux - tar extract files

Here we’re using the following arguments:

  • x – tells tar to extract its archive
  • f – tells tar the location of the file archive

As a result of this operation, we got a wordpress folder.

The process of extracting files in multiple steps is not convenient, so that’s why tar supports additional argument process its archive through gzip. The same operation, but only one command:

tar zxf latest.tar.gz
5. How to Open and Extract .gz files in Linux - tar gunzip and extract files

Here, we’re using an additional argument:

  • z – tells tar to filter its archive through gzip or gunzip

We unzipped files and extracted them from the tar archive using only one single command.

FAQ

How can I unzip tar.gz file?

The simplest and fastest way to unzip a tar.gz file is to put it in a separate folder on your filesystem and execute the following command:

tar zxf my_archive.tar.gz
Unzip tar.gz file demo

How I can untar tar.gz file?

To untar tar.gz file, you need to unpack the tar archive from the zipped tar.gz file and then execute the following command:

tar xf my_archive.tar

The above command does not contain the z (zipped) argument.

Untar tar.gz file demo

How to extract gz file in Python?

To extract gz file (Python), you have to run the Python interpreter and use the os module to execute the tar command from your script. Here’s the Python code example:

import os
file_name = 'my_archive.tar.gz'
os.system('tar zxf ' + file_name)

A complete workflow will look like the following:

Extract gz file Python demo

Summary

In the Linux operating system, the .gz file archives are commonly used in combination with tar archives that allow compressing multiple files at once. This article provided a complete guide on extracting and opening .gz and .tar.gz files in Ubuntu.

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.