Gunzip Command in Linux With Examples

December 21, 2023

Introduction

The gunzip command is a widely used command-line tool that serves as a decompression algorithm. It functions as the counterpart to the gzip command. gunzip restores the compressed file to its original name, owner, mode, and timestamp when executed.

The following text provides a comprehensive overview of the gunzip command in Linux.

Linux gunzip command with examples

Requirements

  • A Linux system (this tutorial uses Ubuntu 22.04.).
  • Access to the terminal.
  • The gunzip utility installed.

gunzip Command Syntax

The gunzip command syntax is:

gunzip [options] [archive_name]

Note: The compressed file must be compressed using gzip and in the current working directory. If the file is in a different directory, navigate to that directory or use the full path to the file.

The command prints an error message when run without any options or archive name. 

gunzip terminal output

However, running gunzip with an archive name decompresses that archive.

For example, we will decompress the test_file1.gz file:

gunzip test_file1.gz
gunzip test_file1.gz terminal output

The command has no output. Run the ls command to confirm the file is not compressed anymore.

ls terminal output showing uncompressed file

The output shows test_file1, a decompressed text file. 

Note: When running the command, you are allowed to use the archive_name without the .gz suffix if it's the only file with that name. The command recognizes it is as an archive. However, if there are differenet file types with the same name, use archive_name with the .gz suffix.

gunzip Command Options

The gunzip command has plenty of options that modify it and produce tailored output. The common ones are presented in the table below:

OptionDescription
-c, --stdoutKeeps original files unchanged but writes the text from a compressed file to the standard output without uncompressing it.
-f, --forceForces decompression even if the conditions are not ideal.
-k, --keepKeeps both the compressed and the original uncompressed file after decompression.
-l, --listProvides info about compressed or uncompressed file contents.
-n, --no-nameDoes not save the original file name and timestamp when decompressing.
-N, --nameSaves the original file name and timestamp when decompressing.
-q, --quietSuppresses all warnings.
-r, --recursiveUnzips files not only in the current directory but also in its subdirectories.
-S, --suffix=SUFUses the suffix '.suf' instead of '.gz' when decompressing.
--synchronousThe data is written to the output file synchronously, where each operation must be completed before the next one begins. The process is slower but considered safer in case of a system crash or failure.
-t, --testPerforms a test by checking the decompressed file integrity.
-v, --verboseProvides a verbose output to give a better understanding of what is happening.
--helpDisplays the help message and exits.
-V,--versionShows the information about the version of gunzip.

gunzip vs. unzip Command

The main distinctions between gunzip and unzip is the supported file types, the compression/decompression processes, and their use cases.

The gunzip tool is designed to decompress files in the GZIP format. GZIP is commonly used to compress large files or groups of files, providing efficient disk space utilization and faster processing.

The unzip utility extracts files from archives in the ZIP format. ZIP archives combine multiple files into a single compressed file for easier transfer and storage. The unzip command reads ZIP archives, extracting compressed files to the original form. It supports various compression methods like deflate, bzip2, and LZMA.

The tools differ in the decompression processes. While gunzip reads and restores data block by block, unzip extracts files from an archive and handles multiple files simultaneously.

gunzip exclusively supports files in the GZIP format, whereas unzip is versatile and handles ZIP files and other compression methods such as deflate, bzip2, and LZMA.

gunzip Command Examples

gunzip has plenty of options available, and there are a lot of use-case examples. The following text presents eight practical examples.

1. Decompress a File

An alternative to running gunzip with no options is to use the -d argument. The output is the same.

The syntax is:

gunzip -d [archive_name]

For instance, decompress test_file1.gz with the following:

gunzip -d test_file1
gunzip -d terminal output


2. Provide Verbose Output

When running gunzip with an archive name but without any options or with -d, the command has no output. The only way to verify the outcome is to list the files in the current working directory or with the -v option to get a verbose output.

The syntax is:

gunzip -v [archive_name]

For instance, apply the command to decompress the archive test_file1.gz:

gunzip -v test_file1.gz
gunzip-v terminal output

The output shows the file is decompressed and changed the extension.

3. List the Contents of the Compressed File

To see the info about an archive without decompressing it, use the -l option:

gunzip -l [archive_name]

For instance, print the information about testfile_1 with:

gunzip -l testfile_1
gunzip -l terminal output

The command doesn't decompress the file, but it does show valuable info about the compressed file and the size and name of the decompressed file.

4. Decompress Multiple Files

The gunzip command can decompress multiple files. To do that, run the command and add archives' names separated by spaces.

The syntax is:

gunzip [arhcive_name_1] [arhcive_name_2] [arhcive_name_3]

For example, we have three archives: test_file1.gz, test_file2.gz, and test-file3.gz, as shown by the ls command output:

ls
ls terminal output multiple archoves

Decompress all three files with the following:

gunzip -v test_file1 test_file2 test_file3
gunzpip -v multiple files

The gunzip -v shows all three archives are decompressed.

5. Force Decompress a File

Using the -f option with gunzip forces the file decompression, even if it has a questionable or missing gzip header.

By default, gunzip checks for a valid gzip header in the compressed file to ensure it is a valid gzip-compressed file before attempting decompression. However, sometimes, the header might be missing or corrupted, leading to issues with decompression.

In that case, gunzip is not able to decompress the file. For instance, run gunzip on an unsuitable file, in this case, called corrupted_file.gz:

gunzip corrupted_file.gz
gunzip corrupted file terminal output

Even though the file has the .gz extension, it is corrupted and gunzip does not extract the contents. The -f option overrides this, allowing the decompression to proceed. For instance:

gunzip -f corrupted_file.gz

gunzip -f terminal output

This is useful when users suspect the file is compressed but does not have the correct header information or if the file extension does not match the actual compression format.

6. Keep the Compressed File

The gunzip tool, by default, deletes the compressed file once it is decompressed. To keep the compressed file, run the following command:

gunzip -k [archive_name}.

For instance, decompress test_file1.gz, but keep the archive:

gunzip -k test_file1
gunzip -k terminal output

Run ls to verify both the archive and the file are present.

ls
ls after gunzip -k terminal output

7. Test the File Is Valid

The -t option in the gunzip command tests the integrity of a compressed file. It allows users to check if the compressed file has a valid gzip format without decompressing it. This process is helpful to ensure the file is not corrupted before attempting decompression.

The syntax is

gunzip -t [archive_name]

For instance, test whether the archive test_file1.gz is valid with:

gunzip -t test_file1
gunzip -t valid terminal output

The output is empty, indicating the file is valid. If the command is executed on a non-valid file, like a text file, the output is different:

gunzip -t invalid terminal output

8. Read Text Within a Compressed File

The gunzip command has the -c option that allows users to read the compressed file text without decompressing it.

The syntax is:

gunzip -c [archive_name]

For instance, read the contents of the archive test_file1.gz with:

gunzip -c test_file1
gunzip -c terminal output

Conclusion

After reading this article, you know how to use the Linux gunzip command with the help of several practical examples.

Next, learn how to extract tar.gz files from the Linux terminal.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Unzip tar.bz2 Files
July 13, 2023

Tar.bz2 files combine the archiving capabilities of tar and the BZ2 compression algorithm. Learn how to unzip tar.bz2 files and how to work with compressed archives in this guide...
Read more
14 Dangerous Linux Terminal Commands
November 17, 2021

It is always dangerous to run a Linux terminal command when you aren't sure what it does. This article lists 14 Linux commands that can have adverse...
Read more
Linux Commands Cheat Sheet {with Free Downloadable PDF}
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need...
Read more
How to Unzip BZ2 File
July 11, 2023

BZ2 files have higher compression rates and relatively low decompression speeds. This simple step-by-step guide will show you how to unzip BZ2 files on Linux and Windows...
Read more