gunzip Command: Tutorial & Examples

Decompress gzip-compressed files

The gunzip command is a standard Linux utility used to decompress files compressed with gzip. It reverses the compression process by reading .gz files and restoring the original uncompressed file. This tool is essential for managing compressed data efficiently on servers, especially when working with large log files, backups, or archives.

How gunzip Works

gunzip operates by decompressing files compressed using the gzip format. The gzip format is a widely-used compression algorithm that reduces file size for storage and transfer efficiency. When you run gunzip on a .gz file, it reads the compressed data, decompresses it, and by default replaces the .gz file with the decompressed version, restoring the original filename.

For example, decompressing example.txt.gz will remove the .gz extension and produce example.txt. Internally, gunzip uses the DEFLATE algorithm to restore the original data.

What gunzip Does

  • Reads one or more .gz compressed files.
  • Decompresses each file to its original uncompressed state.
  • By default, deletes the compressed .gz file after successful decompression.
  • Supports decompressing multiple files at once.
  • Provides options to control output behavior such as keeping the original compressed file or writing decompressed data to stdout.

Why gunzip Is Important

In Linux server environments, space and bandwidth optimization is critical. Compressing files reduces disk usage and speeds up file transfer. However, before using these files, such as logs or backups, they must be decompressed. gunzip provides a simple, reliable way to decompress gzip files, enabling administrators to manage storage efficiently and access data quickly.

Common scenarios where gunzip is critical include:

  • Accessing archived logs in /var/log for troubleshooting or auditing.
  • Extracting backup files for restores.
  • Preparing compressed downloads or data for processing.
  • Automating decompression in scripts and cron jobs.

Typical Problems Solved By gunzip

  • Recovering original files from .gz compressed archives.
  • Decompressing logs for analysis or monitoring.
  • Extracting compressed configuration or data files before use.
  • Resolving issues caused by disk space shortages by enabling files to be compressed and decompressed as needed.
  • Handling compressed files downloaded from the internet or transferred between systems.

Common gunzip Parameters

Here are some of the most useful parameters you can use with gunzip:

  • -k — Keep the original compressed file after decompression.
  • -l — List information about the compressed file without decompressing.
  • -v — Verbose mode; shows detailed information about the decompression process.
  • -f — Force decompression even if the output file exists or the compressed file has multiple links.
  • -c — Write decompressed output to stdout, keep original file intact.
  • -t — Test the integrity of the compressed file without decompressing.
  • -r — Recursively decompress .gz files in directories.
  • --fast / --best — Control compression speed if used with gzip (mostly relevant when combined with gzip).

Practical Examples Using gunzip

gunzip file.gz

Decompresses file.gz and replaces it with file.

gunzip -k file.gz

Decompresses file.gz but keeps the original .gz file intact.

gunzip -l file.gz

Lists information about file.gz without decompressing. Sample output:

compressed        uncompressed  ratio uncompressed_name
10240             20480         50.0% file

This shows the compressed size, uncompressed size, compression ratio, and original file name.

gunzip -v file.gz

Verbose output during decompression, for example:

file.gz:    73.3% -- replaced with file

gunzip -c file.gz > file

Decompresses file.gz to stdout and redirects output to file, keeping the original compressed file.

gunzip *.gz

Decompresses all .gz files in the current directory.

gunzip -r /var/log

Recursively decompresses all .gz files in /var/log and its subdirectories.

Common Errors And Troubleshooting

  • gunzip: file.gz: unexpected end of file

    Indicates the compressed file is corrupted or truncated.

  • gunzip: file.gz already exists.

    Occurs when the decompressed file exists and gunzip refuses to overwrite unless -f is used.

  • Permission denied

    Happens when the user does not have read/write permissions on the file or directory.

To troubleshoot, verify file integrity using gunzip -t file.gz, check permissions, and ensure enough disk space is available.

Scripting And Automation With gunzip

gunzip is frequently used in scripts to automate decompression tasks. For example, to decompress all .gz files in a directory and log the operations:

for f in /backup/*.gz; do
    gunzip -v "$f" >> /var/log/gunzip.log 2>&1
done

This loop decompresses each file and appends verbose output to a log file.

For automated cron jobs, combining gunzip with other commands can manage backups or log rotation efficiently.

Security Considerations

When decompressing files from untrusted sources, be cautious of decompression bombs—maliciously crafted compressed files that expand to enormous sizes and consume system resources. Always verify file origins and use resource limits where possible.

Avoid overwriting important files accidentally by not using -f unless intentional. Use -k or -c if you want to preserve originals.

Comparison With Alternatives

  • gzip -d is functionally equivalent to gunzip and often interchangeable.
  • zcat decompresses .gz files to stdout, useful for piping without writing files.
  • unzstd decompresses .zst files, which often provide better compression ratios and speeds compared to gzip.

Cheatsheet For gunzip Options

  • gunzip file.gz — Decompress and replace.
  • gunzip -k file.gz — Keep compressed file.
  • gunzip -l file.gz — List compressed file info.
  • gunzip -v file.gz — Verbose output.
  • gunzip -f file.gz — Force overwrite.
  • gunzip -c file.gz > file — Decompress to stdout.
  • gunzip -r /path — Recursively decompress in directory.
  • gunzip -t file.gz — Test compressed file integrity.

Recommendation: zstd

While gunzip is a great tool for decompressing files, we recommend using zstd for even better compression and decompression speeds. It's a powerful compression algorithm, particularly useful for large files.

To decompress a .zst file, you would use the unzstd command, similar to how you use gunzip for .gz files:

unzstd file.zst

This decompresses the file.zst and replaces it with file.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA