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, it means these files were compressed with 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. After you compress a file using gzip, you'll need to decompress the file. Here's how to decompress a file that was compressed using the gzip command.

Decompress Files Using the gzip Command

The gzip command provides a method to decompress files with the .gz extension.

To decompress a file, use a minus d (-d) switch as follows:

gzip -d myfilename.gz

The file is decompressed, and the .gz extension is removed.

Linux gzip decompress file

Decompress a File Using the gunzip Command

While using the gzip command is valid, it's easier to remember 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 gunzip refuses to decompress a file is where the filename that is left after decompression is the same as one that already exists.

For example, imagine you have a file called document1.doc.gz, and you want to decompress it using the gunzip command. Imagine you also have a file called document1.doc in the same folder. When you run the following command, a message appears and states that the file exists. You are prompted to confirm the action.

gunzip document1.doc.gz

You can enter Y to accept that the existing file will be overwritten. If you implement gunzip as part of a script, however, 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 overwrites an existing file of the same name, and it doesn't prompt you while 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 decompresses the file, and the extension is removed. Therefore, a file called myfile.gz will be called myfile, and it will be expanded to its full size.

When you want to decompress the file but also keep a copy of the compressed file, run the following command:

gunzip -k myfile.gz

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

Linux gunzip keep archive

Display Compressed Output

If the compressed file is a text file, you can view the text in it without decompressing it first. To do this, use the following command:

gunzip -c myfile.gz

The above command displays 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 that is low on disk space.

Imagine you have a drive that 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.

Decompress 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, 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 receive a message. Otherwise, you are returned to the input with no message.

What Happens When You Decompress a File

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

If you want to see more information, 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 compressed filename, how much it was decompressed, and the final filename.

Was this page helpful?