Gunzip Command in Linux

FREE Online Courses: Enroll Now, Thank us Later!

In this article, you will learn all there is to the gunzip command in Linux. We will start right from what gunzip is, why it is used and the file format related to gunzip.

We shall also be going technical, by looking at the syntax of the gunzip command, and discussing the various options used along with the output from the terminal. At the very end, we shall also be looking into a couple of practical examples of the gunzip command in Linux.

What is gunzip in Linux?

Gunzip is a command-line-based utility in Linux based operating system that helps compress or decompress (expanding) a file or list of files in Linux. It is a very simple command that is very commonly used.

In windows, you have the ability to zip or compress files and folders into a single zip file. You can also unzip or decompress the zip files to access the contents in them. See the similarity? Gunzip is to Linux what zip is to windows.

In windows, we know that the zip file had an extension of “.zip”, we also know that Linux is more powerful and better than windows, hence we get the opportunity of compressing files in Linux using gunzip in either “.gz” or “.z”. Gunzip also recognizes files with “.tgz”, “.taz”, “.tar.gz”, and “.tar.Z” extensions.

Gunzip was created by Jean-loup Gailly and Mark Adler. Their program was the replacement for the ‘compress’ program, which was used in the early Unix systems. They released the first version under GNU GPL, making it open source. Version 0.1 of gunzip was publicly released on the 31st of October 1992.

What are the uses of Linux gunzip command?

As we have seen, gunzip is used to compress and decompress files in Linux based operating system. But why do we compress or decompress files? Let us look at the importance and uses of ‘zipped’ files.

Let’s get straight to the point, the very reason why we zip files is that zipped files take up way less storage space and can be transferred to other computers more quickly than uncompressed files making workflow efficient as we save a lot of time and storage using zip files.

Just think about it, sharing i big file is any day better than sharing hundreds of small files. The best part is, that this process is completely reversible, meaning that once the files are shared to another computer, the user can unzip the files and have full control over the file just like you.

Zip files are a boon when it comes to places where you can only send a limited amount of files, like mail, teams, and many more, instead of the single 10 file limit, you can simply attach 1 zip file, with a hundred files!

Syntax of the gunzip command in Linux

Before heading any further let us look at the syntax of the gunzip command to prove a better understanding of the subsequent sections.

The syntax for using the gunzip command is shown below:

gunzip <options> <filename>

Note: gzip is for compressing while gunzip is for decompressing.

How does Linux gunzip work?

As of today, gunzip can decompress files that were compressed by gzip, zip, compress or pack commands. The detection of the input format is automatic.

When you are using the formats gzip and zip, gunzip checks a 32-bit CRC. However, for ‘pack’, gunzip checks the uncompressed length. The typical compress format was not planned to allow consistency checks.

While uncompressing a ‘.Z’ file, if you get an error, do not assume that the ‘.Z’ file is correct simply because the standard ‘uncompress’ does not complain. That said, gunzip sometimes is able to detect a bad ‘.Z’ file.

If a file is compressed by ‘zip’, it can be uncompressed only if they have a single member compressed with the ‘deflation’ method. This feature is only intentional to help conversion of ‘tar.zip’ files into ‘tar.gz’ file format.

Gunzip file format

The gunzip tool is based on the deflate algorithm which was intended as a replacement for LWZ and other data compression algorithms. The deflate algorithm is a combination of Huffman coding and LZ77. Gzip is also referred to as the gzip file format. Let us see what it is:

1. The gunzip file format is a 10-byte header that contains a magic number, the compression method, a 4-byte timestamp, 1 byte of header flags, compression flags, and the ID of the operating system.

2. The file format also consists of optional extra headers as allowed by the header flags, including the original filename, the lower half of a CRC-32 checksum for the header section,an ‘extra’ field, and a comment field

3. Gunzip is a body, which contains a DEFLATE-compressed payload.

4. Gunzip is an 8-byte footer, containing the length of the original uncompressed data and a CRC-32 checksum.

Gzip is mostly used to compress just single files. The final compressed file mostly typically has the ‘.tar.gz’ or ‘.tgz’ extensions. However, since we are only focusing on gunzip (decompressing files), let us keep gzip for another article.

Options used with the gunzip command

We can use different options in the syntax of the gunzip command to specify the way the output or decompression of a file happens. Let us look at some of the most commonly used options:

1. -a

This option converts end-of-lines using local conventions. This command can also be written as ‘–ascii’. Please note that this option is supported only on some non-Unix systems.

2. -c

This option writes output on standard output. It keeps original files unchanged.

3. -d

This command decompressed the files. It can also be written as ‘–decompress’ or ‘–uncompress’

4. -f

This command forces decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal. This command can also be written as “–force”

5. -L

This option displays the information about the gunzip license.

6. -n

This option does not save the original file name and timestamp when decompressing it.

7. -N

This option is the exact opposite of the option ‘-n’, it saves the original file name and timestamp when decompressing it.

8. -q

This option suppresses all the warnings.

9. -r

This option ravel the directory structure recursively. If it finds any of the file names specified on the terminal are directories, gunzip will go into the directory and uncompress all the files in it.

10. -S

This option uses the suffix ‘.suf’, instead of ‘.gz’ when decompressing.

11. -t

This option performs a test by checking the integrity of the decompressed file.

12. -v

This option provides a verbose to give a better understanding of what is happening.

13. -V

This option displays the information about the version of gunzip you are using.

14. -h

This option displays the help menu, with the different options. If at all you get stuck anywhere anytime using the gunzip command, just type the command “gunzip -h” or “gunzip –help”

help option

Now that you have known the basic information of gunzip and have also gone through the syntax and different options used. Let us now use this information to look into some of the practical examples of the gunzip command in the terminal.

Decompressing a file using the gunzip command

To decompress a file, you obviously need to have an already compressed file. To decompress, use the command:

gunzip <filname.gz>

decompressing a file using the gunzip command

Providing verbose while decompressing

In the above output, there is absolutely no proof of checking if the file has been decompressed or not, except if we type the ‘ls’ command and check the extension. It would be really helpful if we have some verbose, thankfully we can by using the option ‘-v’.

providing verbose while decompressing

Forcing gunzip to decompress a file

To force the decompressing of the file, use the option ‘-f’ like this:

gunzip -f <filename.gz>

Listing the contents of the compressed file

If you want the terminal to show you information about the compressed file, you can use the option ‘-l’. Note that this command will not decompress the file, but only show you the contents.

listing the contents of the compressed file

If the data shown in the above output is not enough, you could always ask for even more verbose by using the option ‘-v’ as shown:

gunzip -v (filename.gz)

listing contents of the compressed file

Keeping both compressed and decompressed files.

If you wish to keep the compressed file even after decompressing it, you can do it by using the option ‘-k’ as shown:

gunzip -k (filename.gz)

keeping both compressed and decompressed files

Decompressing multiple files

You can decompress multiple files using gunzip by simply writing the “.gz” filenames separated by a space as follows:

gunzip <file1.gz> <file2.gz> <file3.gz> <file4.gz>

decompressing multiple files

Recursively decompressing files in a directory

If you use the option ‘-r’, gunzip will recursively unzip all of the files in it. If the directory you specify also has subdirectories, the command will go in and decompress even the files in the subdirectories.

Displaying License of gunzip

Use the option ‘-L’ to display the license information of gunzip

displayin license of gunzip

Testing if a file is valid or not

To test if a file is valid for decompression or not, use the option ‘-t’

testing if a file is valid or not

Viewing text within a compressed file

If you want to view the text of the compressed file without uncompressing it, use the option “-c” as shown:

gunzip -c <filename.gz>

viewing text within a compressed file

Viewing information about a file

To get information about either the compressed or uncompressed file, use the option “-l” as shown:

gunzip -l <filename.gz>

viewing information about a file

Viewing version of gunzip

To display information about the version of gunzip, use the option “-V” as shown:

gunzip -V

viewing version of gunzip

Simply decompressing a file

To simply decompress a file, use the option “-d” as shown:

gunzip -d <filename.gz>

simply decompressing a file

Gunzip vs Gzip

Before we end, let us settle the difference between these 2 very common commands – Gzip and Gunzip. Both these commands are exactly the same from head to toe, with only one single difference.

The gzip command is used to zip files and the gunzip command is used to unzip files. It is that simple! If you want more information on the gzip command, feel free to read my article on it.

Summary

As you have seen, linux gunzip command is a really simple tool that helps in the efficiency of workflow. The simple process of decompressing a file or directory with just a simple command is simply god sent! The way we can use gunzip with the many options it comes with makes the process of decompressing files so much easier.

You have now learned what gunzip is, the different uses of it, and how the tool works. We have also gone deep to understand the file format of gunzip along with the syntax and different options used with the command. In the end, we have also looked at some practical examples of decompressing files.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *