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.
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]
The command prints an error message when run without any options or archive name.
However, running gunzip
with an archive name decompresses that archive.
For example, we will decompress the test_file1.gz file:
gunzip test_file1.gz
The command has no output. Run the ls
command to confirm the file is not compressed anymore.
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:
Option | Description |
---|---|
-c , --stdout | Keeps original files unchanged but writes the text from a compressed file to the standard output without uncompressing it. |
-f , --force | Forces decompression even if the conditions are not ideal. |
-k , --keep | Keeps both the compressed and the original uncompressed file after decompression. |
-l , --list | Provides info about compressed or uncompressed file contents. |
-n , --no-name | Does not save the original file name and timestamp when decompressing. |
-N , --name | Saves the original file name and timestamp when decompressing. |
-q , --quiet | Suppresses all warnings. |
-r , --recursive | Unzips files not only in the current directory but also in its subdirectories. |
-S , --suffix=SUF | Uses the suffix '.suf' instead of '.gz' when decompressing. |
--synchronous | The 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 , --test | Performs a test by checking the decompressed file integrity. |
-v , --verbose | Provides a verbose output to give a better understanding of what is happening. |
--help | Displays the help message and exits. |
-V ,--version | Shows 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
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
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
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
Decompress all three files with the following:
gunzip -v test_file1 test_file2 test_file3
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
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
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
Run ls
to verify both the archive and the file are present.
ls
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
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:
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
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.