
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll discuss how to use gunzip for decompressing files in Linux.
Gzip compressed files are compressed using the Lempel-Ziv encoding also known as LZ77. Files in gzip format are usually named the same as the original, just with .gz appended.
Unlike some other compression formats, gzip only supports one file per archive. To compress multiple files, gzip can be used together with tar, a utility to pack files in a single, uncompressed archive. We can recognize these archives through the .tar.gz file extension.
Decompressing a gzip archive is fairly straightforward:
$ gunzip file.txt.gz
This will yield the uncompressed file.txt and by default, gunzip deletes the archived file.
We can choose to preserve the archive after decompression using the -keep or -k options:
$ gunzip -keep file.txt.gz
After decompressing this way, we end up with both the file.txt.gz and the uncompressed file.txt.
This feature is available to gunzip in version 1.6 which may not be the version of gunzip that’s shipped with our Linux distribution.
To decompress multiple files in one go, we add them to the same command used above:
$ gunzip file-1.txt-gz file-2.txt.gz file-3.txt.gz
Alternatively, we can, of course, use a wildcard to extract all archives in the current directory:
$ gunzip *.gz
Finally, we can tell gunzip to traverse a directory recursively and decompress all archives along its way.
For example, we can decompress all archives in the current directory and all subdirectories:
$ gunzip -r .
In this article, we’ve seen how to use gunzip to decompress gzip-compressed archives.
The gzip format has one limitation, it supports single files only. To compress and decompress multi-file archives, we have to combine it with a utility like tar.