Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

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.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

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.

3. Decompressing

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.

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:

$ gunzip *.gz

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 .

7. Conclusion

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.