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

Andrei Maksimov

Andrei Maksimov

0
(0)

When transferring files over the network, it’s more preferable to transfer a single file (usually .gz or .tar.gz archive). Such an approach allows minimizing disk IO operations and speed 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 a file archives in Linux:

  • gzip
  • tar

‘gzip’ command

gzip is the most commonly used tool in the Linux world that 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 allows 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?!

The difference between ‘gzip’ and ‘tar’

gzip is an archival utility that is responsible for the compression of the file, 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 file to a single file which is called “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.

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.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
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.