The gunzip command in Linux: decompress gzip files

Introduction

In the Linux world, file compression and decompression is a everyday task. The gzip format, identified by the extension .gz, is one of the most used due to its speed and reasonable compression ratio. To reverse that process and obtain the original file, the gunzip command is used. In this article we will explain how it works, its most useful options, and some practical examples.

What is gzip and why use gunzip?

gzip is a compression program that applies the Deflate algorithm. When you compress a file with gzip, you obtain a reduced version with the extension .gz. The gunzip command performs the inverse operation: it reads the compressed file, decompresses it, and by default deletes the .gz file, leaving only the original file. This feature is useful for saving space after extraction.

Basic Syntax

The simplest way to use gunzip is:

gunzip filename.gz

This command decompresses filename.gz and produces filename in the same directory. If the original file already exists, gunzip will ask for confirmation before overwriting it, unless the -f (force) option is used.

Most Used Options

  • -c or --stdout: writes the result to standard output without deleting the .gz file. Useful for pipelines.
  • -d: explicitly indicates decompression (it is the default behavior of gunzip, but included for clarity).
  • -f: forces decompression, overwriting existing files without prompting.
  • -k: keeps the .gz file after decompression (does not delete it).
  • -l: lists information about each compressed file, such as compressed size, uncompressed size, and compression ratio.
  • -v: verbose mode, shows the name of each processed file and the compression percentage.
  • -r: acts recursively on directories, decompressing all .gz files it finds.

Practical Examples

Suppose you have a compressed log file named syslog.gz. To decompress it while keeping the original file, use:

gunzip -k syslog.gz

If you want to view the content directly in the terminal without creating a file, you can combine gunzip with less or more:

gunzip -c syslog.gz | less

To decompress all .gz files inside a directory and its subdirectories:

gunzip -r /var/log/

If you only need to obtain information about the compression level without extracting, use:

gunzip -l *.gz

This will display a table with the compressed size, original size, and compression ratio for each file.

Troubleshooting Common Issues

One of the most frequent errors is attempting to decompress a file that is not in gzip format. In that case, gunzip will return a message like gunzip: file.gz: not in gzip format. Verify that the file actually has the .gz extension and is not corrupted. Another situation occurs when disk space is insufficient; gunzip will fail indicating no space left on device. Free up space or choose a directory with more capacity.

If the compressed file is protected against overwriting and you wish to force the operation, add the -f flag. Be careful, as this will delete any existing file with the same name without warning.

Conclusion

The gunzip command is an essential tool for any Linux user working with compressed files. Its simple syntax, combined with options like -k, -c, and -r, allows it to be adapted to multiple scenarios, from quickly extracting a single file to massive log processing on servers. Mastering gunzip improves efficiency in data management and facilitates access to information stored in gzip format.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .