In this tutorial, we’ll discuss how to use gunzip for decompressing files in Linux.
2. Gzip Format
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:
This will yield the uncompressed file.txt and by default, gunzip deletes the archived file.
4. Preserving Archives
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.
5. Decompressing Multiple Files
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:
6. Decompressing Recursively
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.