The gunzip command in Linux is used to unzip files that have been compressed using the “gzip” format. As such, it can’t unzip files that use a different compression mechanism like say, rar files or even the “zip” files that we see on Windows. Here, I’ll show you how to use the gunzip command to carry out various operations, as well as an exploration of what makes gunzip different from other Linux commands.
How to Use Gunzip
Using gunzip is easy. To demonstrate this, I’ll first create a zip file using the well-known gzip command.
Creating a Zipped File
First, we create a file:
echo "This is a sample text file for demonstration purposes." > sample.txt
Then we use the gzip command like this:
gzip sample.txt
You can see below that we’ve created the “sample.txt.gz” file:
Note that the original “sample.txt” file has been deleted. This is an important point to note when it comes to the differences in the uses of these tools between Linux and Windows. In Windows, when you compress a file using “zip”, the original file remains. But in Linux, by default, these tools remove the uncompressed file. As we’ll see, there are options to prevent this.
Using gunzip to Decompress
Now that we’ve created our gzipped file, we can decompress it like this:
To decompress the gzip file we just created, we use gunzip like this:
gunzip sample.txt.gz
This will restore “sample.txt” and delete the compressed file as seen here:
You can see above, that gunzip has restored the original file with its original contents. At a basic level, this is all there is to know about gunzip.
But of course, it’s more than that.
Useful Flags to Know with Gunzip
Using gunzip without any flags is the easiest way to perform default operations. But you can customize its behavior in a variety of ways to suit the situation.
Keeping the Original gzipped File
For example, you can use the “-k” or “–keep” flag to ensure that gunzip doesn’t delete the original gzipped file like this:
gunzip -k filename.gz
A good example of when you might want to keep the original file is if they are archives for say, data analysis. When you need to analyze the archives, you have to decompress them, but deleting them would be disastrous.
Redirecting gunzip to Standard Output
By default, the output of gunzip goes towards creating a new file. If you want, however, you can change this behavior using the “-c” flag, which redirects the output towards the standard output. You can then pipe this output to a regular file, or even to grep to search for something specific like this:
gunzip -c server_log.gz | grep "ERROR"
The above command will show all the lines from “server_log.gz” containing the word “ERROR”. Because we redirected the output, there is no decompressed “server_log” on the disk afterward. Crucially, the “-c” flag doesn’t delete the original gzipped file, making it a safe way to explore the contents of gzipped files without worrying about deleting them or creating any new files on disk.
Force Decompression
There are situations where gunzip will refuse to decompress a file. For example, if a file already exists with the same name as the one that gunzip will create while decompressing, the operation will fail. Using the “-f” or “–force” flag, however, will cause gunzip to overwrite the destination file with the newly created one.
Of course, you can always manually delete the file before running gunzip, but why go through the bother of creating an extra step, when one will do? Particularly if it’s an operation you repeat frequently.
Here’s how to use gunzip using the “–force” option:
gunzip -f report.txt.gz
However, there is another situation where gunzip will refuse to decompress a file
Decompressing Files with a Different Extension
Apart from refusing to overwrite an existing file – which we can override with the “-f” flag – gunzip will also refuse to decompress files that don’t have the “.gz” extension. Using the “-f” flag in this case won’t work. Instead, you need to use the “-S” parameter, which allows you to specify a different extension. Like this:
gunzip -S .zipped sample.zipped
In the above command, I have a file called “sample.zipped”. I know that this file was created by the gzip tool, but for one reason or another, the file extension is different. The above command, using the “-S” flag followed by the “.zipped” extension, tells gunzip to attempt to decompress the file even though it doesn’t have the correct extension. Here’s a screenshot:
You can see above that gunzip first refuses to decompress the “.zipped” file, but then complies once I supply the “-S” parameter and specify the extension.
Decompressing Multiple Files
Using gunzip, you can decompress multiple files at the same time. Let’s say you have a directory of “.gz” files and you want to extract them all at once. You can use the following command:
gunzip *.gz
This will convert all the compressed files in the directory into uncompressed ones. As usual, remember that the original files will be lost, so use caution!
Comparing gunzip to Other Tools
Gunzip isn’t the only way to decompress files. Two other tools in Linux compete with it for doing the same thing. Here’s how it stacks up against each.
Gunzip vs gzip -d
Gunzip and “gzip -d” have the same functionality. In fact, on some installations, gunzip is merely a symbolic link to the gzip -d command. On others like Ubuntu, however, the two commands are different, though they also have identical functionality. For me personally, I find that using gunzip is slightly more natural to use than gzip -d, but this will vary from person to person.
Gunzip vs tar
In Linux, compression often goes hand in hand with archiving. Archiving is a process where you take all the files and folders and combine them into a single file while preserving the directory structure, and file permissions – a snapshot of the entire project in a single file. This archive has the extension “tar”, but it isn’t compressed automatically. In practice, they undergo a separate compression step, and so we get the “.tar.gz” extension.
If you have a file with the “.tar.gz” extension, you have two options:
- Use a single “tar” command to decompress the tar file and extract the archive
- First use gunzip (or gzip -d) to extract the archive and then extract the contents of that archive.
Naturally, the first option is preferable since it requires two steps instead of one. Since we can use the tar command for both creating the “.tar.gz” file as well as extracting it, it makes sense for the first command to be common. You can use the tar command to extract like this:
tar -xvzf sample.tar.gz
However, tar cannot unzip a “pure” gzipped file that doesn’t include an archive inside it. If your file doesn’t contain an underlying archive, you have no choice but to use a dedicated decompression tool like gunzip or gzip -d.
So while you can use the “tar” command on “.tar.gz” files, you can’t use it on plain “.gz” files.
Conclusion
As you can see, gunzip is an easy tool to use. The options are straightforward, and you just need to be careful not to accidentally delete the original file when you didn’t mean to. But it’s best to stick with the tar command when dealing with archives.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply