Linux gunzip Command

Decompress Gzip Files

Overview

The gunzip command is used to decompress files that were compressed with gzip. It's the counterpart to the gzip command and restores compressed files to their original form. By default, gunzip removes the compressed file after successful decompression.

Key Features:
  • Decompress gzip (.gz) files
  • Preserve file permissions and timestamps
  • Test file integrity before extraction
  • Handle multiple files simultaneously
  • Support for various compression formats

Syntax

gunzip [OPTIONS] [FILE...]

Common Options

Option Description
-k Keep original compressed file
-f Force decompression, overwrite existing files
-t Test compressed file integrity
-v Verbose mode - show compression ratios
-l List compressed file information
-c Write output to stdout, keep original file
-r Recursively decompress directories
-q Quiet mode - suppress warnings

Basic Examples

Simple Decompression

gunzip file.txt.gz
# Decompresses file.txt.gz to file.txt
# Original .gz file is removed

Keep Original File

gunzip -k file.txt.gz
# Creates file.txt while keeping file.txt.gz

Decompress to Standard Output

gunzip -c file.txt.gz
This is the content of the decompressed file
displayed on the terminal...

Test File Integrity

gunzip -t file.txt.gz
# No output means file is valid
# Error message appears if file is corrupted

Advanced Examples

Verbose Decompression

gunzip -v file.txt.gz
file.txt.gz: 65.2% -- replaced with file.txt

List File Information

gunzip -l file.txt.gz
compressed uncompressed ratio uncompressed_name
1024 2944 65.2% file.txt

Force Overwrite

gunzip -f file.txt.gz
# Overwrites existing file.txt without prompting

Multiple Files

gunzip *.gz
# Decompresses all .gz files in current directory

Recursive Decompression

gunzip -r directory/
# Recursively decompresses all .gz files in directory and subdirectories

Practical Use Cases

1. Log File Analysis

# Decompress log files for analysis
gunzip -k /var/log/syslog.1.gz

# View compressed log without extracting
gunzip -c /var/log/syslog.1.gz | grep "error"

2. Backup Restoration

# Test backup integrity
gunzip -t backup.tar.gz

# Extract backup with verbose output
gunzip -v backup.tar.gz

3. Database Dumps

# Decompress database dump
gunzip database_dump.sql.gz

# Pipe decompressed data directly to database
gunzip -c database_dump.sql.gz | mysql -u user -p database

4. Source Code Archives

# Extract source code archive
gunzip source-code.tar.gz
tar -xf source-code.tar

# Or combine with tar
tar -xzf source-code.tar.gz

Working with Different Formats

Supported File Extensions

Extension Description Example
.gz Standard gzip format file.txt.gz
.Z Compress format file.txt.Z
.tgz Gzipped tar archive archive.tgz
.tar.gz Gzipped tar archive archive.tar.gz

Format Detection

# gunzip automatically detects format
gunzip file.txt.Z
gunzip archive.tgz
gunzip data.tar.gz

Tips and Best Practices

Performance Tips:
  • Use -t to test file integrity before decompression
  • Use -c with pipes to avoid creating temporary files
  • Use -k to keep originals when disk space allows
  • Combine with other commands using pipes for efficiency
Common Pitfalls:
  • By default, gunzip removes the original compressed file
  • Existing files are overwritten without warning (use -f explicitly)
  • Corrupted files may partially decompress - always test first
  • Some systems may not have gunzip - use gzip -d instead

Related Commands

Compression Tools:
  • gzip - Compress files with gzip
  • zcat - View compressed files without extracting
  • zless - Page through compressed files
Archive Tools:
  • tar - Archive and extract tar files
  • unzip - Extract ZIP archives
  • 7z - Handle 7-Zip archives

Frequently Asked Questions

The gunzip command decompresses files that were compressed with gzip. It restores the original file and by default removes the .gz extension and the compressed file.

Use the -k option: gunzip -k filename.gz. This keeps the original .gz file while creating the decompressed version.

Yes, use gunzip -c filename.gz to output the decompressed content to stdout, or use zcat filename.gz which is equivalent and more commonly used for this purpose.

They are functionally identical. gunzip is actually a script or link to gzip -d. Both decompress gzip files with the same options and behavior.

Use gunzip -t filename.gz to test the file integrity. If the file is valid, no output is shown. If corrupted, an error message will be displayed.