Example Uses of the "gunzip" Command

Unzip your GZIP files from the Linux command line

If you look through your folders and find files with an extension of ".gz" then it means they have been compressed using the gzip command.

The gzip command uses the Lempel-Ziv (ZZ77) compression algorithm to reduce the size of files such as documents, images, and audio tracks.

Of course, after you have compressed a file using gzip, you will at some stage want to decompress the file again.

In this guide, we will show you how to decompress a file which has been compressed using the gzip command.

Decompress Files Using the gzip Command

The gzip command itself provides a method for decompressing files with the .gz extension.

In order to decompress a file, you need to use a minus d (-d) switch as follows:

gzip -d myfilename.gz

The file will be decompressed and the .gz extension will be removed.

Linux gzip decompress file

Decompress a File Using the gunzip Command

Whilst using the gzip command is perfectly valid, it is much easier to remember just to use "gunzip" to decompress a file as shown in the following example:

gunzip myfilename.gz
Linux gunzip decompress file

Force a File to Decompress

Sometimes the gunzip command has issues with decompressing a file.

A common reason for gunzip refusing to decompress a file is where the filename that will be left after decompression is the same as one that already exists.

For instance, imagine you have a file called "document1.doc.gz" and you wish to decompress it using the gunzip command. Now imagine you also have a file called "document1.doc" in the same folder.

When you run the following command a message will appear stating that the file already exists and you will be asked to confirm the action.

gunzip document1.doc.gz

You can, of course, enter Y to accept that the existing file will be overwritten. If you are implementing gunzip as part of a script, however, then you won't want a message to be displayed to the user because it stops the script from running and requires input.

You can force the gunzip command to decompress a file by using the following syntax:

gunzip -f document1.doc.gz
Linux gunzip force

This will overwrite an existing file of the same name and it will not prompt you whilst doing so. You should make sure, therefore, that you use the minus f (-f) switch carefully.

How to Keep Both the Compressed and Decompressed File

By default, the gunzip command will decompress the file and the extension will be removed. Therefore a file called "myfile.gz" will now be called "myfile" and it will be expanded to full size.

It may be the case that you want to decompress the file but also keep a copy of the compressed file.

Linux gunzip keep archive

You can achieve this by running the following command:

gunzip -k myfile.gz

You will now be left with "myfile" and "myfile.gz".

Displaying Compressed Output

If the compressed file is a text file, then you can view the text within it without having to decompress it first.

To do this use the following command:

gunzip -c myfile.gz

The above command will display the contents of myfile.gz to the terminal output.

Display Information About the Compressed File

You can find out more information about a compressed file using the gunzip command as follows:

gunzip -l myfile.gz
Linux gunzip file info

The output of the above command shows the following values:

  • Compressed Size
  • Uncompressed Size
  • Ratio
  • Uncompressed Filename

The most useful aspect of this command is when you are dealing with large files or a drive which is low on disk space.

Imagine you have a drive which is 10 gigabytes in size and the compressed file is 8 gigabytes. If you blindly run the gunzip command, then you might find that the command fails because the uncompressed size is 15 gigabytes.

By running the gunzip command with the minus l (-l) switch, you can verify that the disk that you are decompressing the file to has enough space. You can also see the file name that will be used when the file is decompressed.

Decompressing Lots of Files Recursively

If you want to decompress all the files in a folder and all the files in all of the folders below that, you can use the following command:

gunzip -r foldername
Linux gunzip directory

For example, imagine you have the following folder structure and files:

  • Documents
    • mydoc.gz
    • mydoc1.gz
    • Accounts
      • spreadsheet1.gz
      • spreadsheet2.gz

You can decompress all of the files by running the following command:

gunzip -r Documents

Test Whether a Compressed File Is Valid

You can test whether a file has been compressed using gzip by running the following command:

gunzip -t filename.gz

If the file is invalid, you will receive a message; otherwise, you will be returned to the input with no message.

What Exactly Happened When You Decompressed the File

By default when you run the gunzip command, you are just left with a decompressed file without the "gz" extension.

If you want to see more information, you can use the minus v (-v) switch to show verbose information:

gunzip -v filename.gz

The output will be something like this:

filename.gz: 20% -- replaced with filename

This tells you the original compress filename, how much it was decompressed, and the final filename.