13 Practical Examples of Using the Gzip Command in Linux

Compression is a very commonly performed operation by users to save disk space as well as reduce time and bandwidth while transferring large amounts of data over the network using gzip utility.

gzip stands for the GNU zip and it is a very popular compression and decompression utility. One of the primary reasons for its popularity is its high compression ratio and speed, which means the compressed data remains the same after decompressing.

The gzip command uses a deflate algorithm which is a lossless data compression that creates smaller file size to make file transfer much faster, as compared to other compression tools.

In this guide, we are going to discuss gzip command usage with examples in Linux.

gzip Command Syntax

Just like any other Linux command, the gzip command’s syntax is divided into two parts OPTIONS and FILES.

$ gzip [OPTIONS]... [FILES]...

In the above syntax, the OPTIONS are used to alter the behavior of the command whereas the FILES represent the input files.

1. Create a Gzip File in Linux

One of the very common uses of the gzip command is to compress a large file. It is very common to see that, large ISO files or tar bundles are compressed to save disk space.

To compress a single file, we just need to pass the file name to the gzip command. To understand this, let’s use the following command to compress the ISO file:

$ gzip alma-linux.iso

Now, let’s use the ls command to verify that the file has been compressed:

$ ls -l
Create Gzip File in Linux
Create Gzip File in Linux

In the above output, we can see the new compressed file with the name alma-linux.iso.gz. We can also observe that, by default, the gzip command adds a .gz extension to a compressed file.

2. Create Gzip and Keep Original File

In the previous example, we saw how easy it is to compress a file. However, if we observe carefully then we can notice that the gzip command deletes the original file after compressing it.

However, sometimes we want to keep the original file as well. In such cases, we can use the -k option as shown.

$ gzip -k alma-linux.iso
$ ls -l
Gzip Keep Original File
Gzip Keep Original File

In the above output, we can see that the original file is intact even after the compression.

3. View Contents of a .gz File

To view the contents of a compressed .gz file, use the zcat command – which allows you to view the compressed file contents without uncompressing.

To understand this, first, create a simple text file using the redirection operator:

$ echo "zcat example from tecmint.com" > demo
$ gzip demo
$ ls -l
$ zcat demo
View Gzip Contents
View Gzip Contents

In the above output, we can see that the zcat displays the contents of the compressed file without uncompressing it.

4. View Info of a .gz File

Sometimes, we want to display more details about the compressed file. In such a case, we can use the -l option to list the following fields:

  • compressed size – it represents the size of the compressed file in bytes.
  • uncompressed size – it represents the size of the uncompressed file in bytes.
  • ratio – it represents the compression ratio.
  • uncompressed_name – it represents the name of the uncompressed file.

To understand this, let’s execute the following command:

$ gzip -l alma-linux.iso.gz
View Gzip File Info
View Gzip File Info

5. Overwrite Gzip File Without Confirmation

By default, the gzip command works in an interactive way if the compressed file with the same name already exists. To understand this default behaviour, let’s execute the same command from the previous example:

$ gzip -k alma-linux.iso
Gzip File Overwrite Confirmation
Gzip File Overwrite Confirmation

Here, we can see that the gzip command waits for the user input. Now, we can use the 'y' to overwrite the file or 'n' to abort the operation.

This interactive operation is safe and avoids overwriting files by mistake. However, this is not suitable every time. For example, if we are executing the gzip command from the script then the script will wait infinitely for the user input. In such cases, we can use the -f option which overwrites the files forcefully.

Now, let’s execute the same command with the -f option:

$ gzip -f -k alma-linux.iso
Gzip Force Overwrite File
Gzip Force Overwrite File

Here, we can see that now the gzip command works in a non-interactive way.

6. Compress Multiple Files with Gzip

So far we saw how to compress a single file. In a similar way, we can use the gzip command to compress multiple files at once.

So, first, let’s create multiple copies of the file using the following cp command:

$ cp alma-linux.iso alma-linux-1.iso 
$ cp alma-linux.iso alma-linux-2.iso 
$ cp alma-linux.iso alma-linux-3.iso 

Next, let’s compress the three files using the below command:

$ gzip alma-linux-1.iso alma-linux-2.iso alma-linux-3.iso
$ ls -l
Create Multiple Gzip Files in Linux
Create Multiple Gzip Files in Linux

7. Gzip All Files in a Directory

In the previous example, we saw how to compress multiple files. In a similar way, we can also compress all the files from a directory.

To understand this, first let us create a new directory and add a few files into it:

$ mkdir dir-1
$ touch dir-1/file-1.txt dir-1/file-2.txt dir-1/file-3.txt

Now, let’s use the -r option to compress all the files from the dir-1 directory:

$ gzip -r dir-1/
$ ls -l dir-1/
Compress All Files in a Directory
Compress All Files in a Directory

In the above example, the -r option traverses the directory in a recursive fashion.

8. Decompress a Gzip File in Linux

The gzip command allows us to decompress the file using the -d option as shown.

$ gzip -d alma-linux.iso.gz
$ ls -l
Unzip Gzip File in Linux
Unzip Gzip File in Linux

9. Compress Tar File to Gzip

In the previous two examples, we saw how to compress multiple files using a single command. However, we can also observe that the gzip command doesn’t compress these files into a single file. In such cases, first, we can create a tar bundle and then compress it using the gzip command.

So, first, let’s create a tar bundle with multiple files in it:

$ tar cvf sample.tar alma-linux-1.iso alma-linux-2.iso
Create Tar File in Linux
Create Tar File in Linux

Now, let us compress this tar bundle using the following command:

$ gzip sample.tar
$ ls -l
Compress Tar File into Gzip
Compress Tar File into Gzip

10. Speeding Up gzip Compression

So far, we used very small files to demonstrate the usage of the gzip command. However, in real scenarios, the data that needs to be compressed can be really large.

In such cases, we can use the --fast option to reduce the compression time.

$ gzip --fast alma-linux.iso

It is important to note that, the --fast option gives preference to the compression speed over the ratio.

11. Speeding Up Gzip Compression Ratio

In a similar way, we can use the --best option to improve the compression ratio. To understand this, let’s execute the below command:

$ gzip --best alma-linux.iso

Here, we should note that the --best option gives preference to the compression ratio over the speed.

12. Set Gzip Compression Level

We can use an integer argument with the gzip command to regulate the speed of the compression. The valid value for the range is between 1 to 9. The value 1 represents the fasted compression whereas the value 9 represents the slowest compression.

For example, the following command uses the 2 as an argument to improve the compression speed:

$ gzip -2 alma-linux.iso 

It is important to note that, the default compression level in gzip is -6. It prefers high compression at the expense of speed.

13. Change Gzip File Extension Suffix

By default, the gzip command uses the .gz suffix after compressing the file. However, we can override this default behavior using the --suffix option.

For example, we can use the below command to use gnuzip as a suffix:

$ gzip --suffix .gnuzip alma-linux.iso
$ ls -l
Change Gzip File Extension
Change Gzip File Extension

In the above example, we can see the compressed file has a .gnuzip extension.

Conclusion

In this practical guide, we discussed some of the common examples of the gzip command in Linux to compress and decompress files faster.

Do you know of any other best example of the gzip command in Linux? Let us know your views in the comments below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Narendra K
I'm an experienced and passionate software engineer with in-depth experience in Linux, Distributed systems, DevOps, and Cloud. My expertise lies in back-end web development, and the main languages in my tech stack are Java, Spring, Python, and Go. I’m a lifelong learner and an open-source enthusiast.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 thought on “13 Practical Examples of Using the Gzip Command in Linux”

  1. Good article, and suggestion In example 7 you can try creating multiple files using brace expansion.

    $ touch dir-1/file-{1..3}.txt
    

    Also in the ‘ls -l‘ command screenshot is supposed to show the file parameters, try using ‘ls -lh‘ to get fire-size in MB/GB.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.