gunzip Command in Linux: Decompress Gzip Files and Data Streams
Learn gunzip command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
8 min read
Learn gunzip command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
- What gunzip Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
Mastering the gunzip command in linux provides systems administrators and developers with the primary standalone interface for decompressing RFC 1952 Gzip archives. Implemented as an alias, hardlink, or symbolic wrapper around gzip -d, gunzip is designed to restore compressed single-file payloads into their original uncompressed form with maximum fidelity.
The gunzip command in Linux decompresses files created by gzip, zip, or compress, replacing the compressed .gz archive with the original uncompressed file. Run gunzip file.gz to decompress, gunzip -k file.gz to keep the original archive, or gunzip -c file.gz to stream decompressed data directly to stdout.
In production infrastructure, gunzip plays an essential role across database restoration, log rotation auditing, software installation, and telemetry pipeline processing. When working with massive SQL dumps, network packet captures (.pcap.gz), or web logs, gunzip allows you to extract files in-place or stream data directly into downstream analytics pipelines without writing uncompressed bytes to physical disk.
Understanding gunzip's flags—such as -k (--keep) to prevent archive deletion, -c (--stdout) for stream piping, and -t (--test) for integrity validation—ensures your file operations remain fast, safe, and resilient against data corruption.
What gunzip Does and When to Use It
At the operating system level, gunzip reverses the Lempel-Ziv 1977 (LZ77) and Huffman coding stages of the DEFLATE compression algorithm. When invoked, it opens the target file and verifies the 2-byte magic identifier (0x1F 0x8B). If the header indicates valid Gzip format, gunzip allocates an in-memory 32KB circular sliding window buffer to reconstruct literal byte strings from distance-length token pairs.
As data is decompressed and emitted, gunzip calculates a streaming 32-bit Cyclic Redundancy Check (CRC-32). Before finalizing execution, gunzip reads the 8-byte trailer located at the end of the .gz container: the first 4 bytes contain the source file's CRC-32 checksum, and the remaining 4 bytes represent the uncompressed size modulo $2^{32}$. If the computed checksum fails to match the header, gunzip prints an error and preserves the original file, protecting administrators from silent corruption.
Under the hood, gunzip gathers runtime telemetry or dispatches control signals by reading virtual filesystems mounted at /proc and /sys, or by communicating with PID 1 (systemd) over D-Bus. Using flags such as -c, -f, -k, -l turns raw process and service tables into actionable diagnostics during incident response.
Availability and Installation Across Linux Distributions
The gunzip utility is packaged in gzip across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:
Before running administrative tasks or automation scripts, verify whether gunzip is installed and inspect which binary path and version your shell resolves:
type -a gunzip
gunzip --version 2>/dev/null || man -f gunzip
# Illustrative output (reviewed against documentation)
gunzip is /usr/bin/gunzip
gunzip (gzip)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y gzip
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y gzip
# Arch Linux
sudo pacman -S --noconfirm gzip
# Alpine Linux
sudo apk add --no-cache gzip
Command Syntax, Arguments, and Exit Status Codes
gunzip [OPTIONS] [FILE...]
The standard syntax is gunzip [OPTIONS] [FILE...]. If no FILE operands are specified, or if a hyphen (-) is passed, gunzip reads compressed data from standard input (stdin) and writes decompressed data to standard output (stdout).
By default, gunzip deletes the input .gz file upon successful decompression, creating an uncompressed file named after the input file with the .gz extension stripped. To preserve the original archive, supply the -k (--keep) option.
0— Successful decompression; archive decompressed and CRC verified.1— Operational error; archive corrupted, invalid format, or CRC mismatch.2— Warning; file skipped or unrecognized file extension.
Important Options and Flags Reference
| Option / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
-c | --stdout | Write output on standard output; keep original files unchanged. |
-f | --force | Force overwrite of output files and allow decompression of links. |
-k | --keep | Keep (do not delete) input files during decompression. |
-l | --list | List compressed and uncompressed sizes and ratio without extracting. |
-r | --recursive | Operate recursively on directories, decompressing all .gz files within. |
-t | --test | Test the compressed file integrity without extracting to disk. |
-v | --verbose | Display name, percentage reduction, and progress for each file. |
--help | --help | Display help documentation and exit. |
The -c (--stdout) option is essential when decompressing into other tools. Running gunzip -c dump.sql.gz | mysql database pipes the uncompressed SQL stream directly into MySQL, avoiding the need to write a multi-gigabyte uncompressed file to disk.
The -k (--keep) flag prevents accidental deletion of backup archives. In standard Unix gzip/gunzip, omitting -k deletes the source .gz file immediately upon extraction, which can result in lost backups if an uncompressed file is later accidentally removed.
When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.
Practical Basic and Advanced Usage Examples
1. Decompress a File In-Place
Decompress a compressed log archive, replacing the .gz file with the uncompressed original.
gunzip syslog-20261020.log.gz
ls -l syslog-20261020.log
# Illustrative output (reviewed against documentation)
-rw-r--r-- 1 root root 8410294 Oct 20 23:59 syslog-20261020.log
gunzip extracts the original uncompressed file and removes syslog-20261020.log.gz from disk.
2. Decompress While Preserving the Original Archive with -k
Extract an archive while keeping the compressed file intact for permanent storage.
gunzip -kv database_dump.sql.gz
ls -l database_dump.sql*
# Illustrative output (reviewed against documentation)
-rw-r--r-- 1 postgres postgres 42100120 Oct 22 14:00 database_dump.sql
-rw-r--r-- 1 postgres postgres 9821040 Oct 22 14:00 database_dump.sql.gz
The -k flag retains database_dump.sql.gz alongside the extracted database_dump.sql file.
3. Stream Decompressed Data to stdout with -c
Output decompressed text to stdout and pipe directly into grep for inspection.
gunzip -c /var/log/nginx/access.log.2.gz | grep " 500 " | head -n 2
# Illustrative output (reviewed against documentation)
198.51.100.22 - - [20/Oct/2026:10:14:02 +0000] "GET /api/v1/orders HTTP/1.1" 500 241
203.0.113.88 - - [20/Oct/2026:10:14:05 +0000] "POST /checkout HTTP/1.1" 500 182
The -c flag streams output to stdout without modifying or deleting access.log.2.gz.
4. Test Archive Integrity Without Decompressing (-t)
Verify that an archived database backup is uncorrupted by validating its CRC-32 checksum.
gunzip -tv backup_20261022.tar.gz
# Illustrative output (reviewed against documentation)
backup_20261022.tar.gz: OK
The -t flag reads the compressed file, computes the CRC-32 checksum in memory, and reports 'OK'.
5. Recursively Decompress All .gz Files in a Directory (-r)
Decompress all rotated log archives across a nested directory tree in a single command.
gunzip -rv /var/log/archive/
# Illustrative output (reviewed against documentation)
/var/log/archive/2026/10/app.log.gz: replaced with /var/log/archive/2026/10/app.log
/var/log/archive/2026/10/error.log.gz: replaced with /var/log/archive/2026/10/error.log
The -r flag traverses all subdirectories and decompresses any .gz files found.
6. Force Overwrite of Existing Files Using -f
Decompress a file even if an uncompressed destination file already exists on disk.
gunzip -f config_backup.json.gz
# Illustrative output (reviewed against documentation)
# Overwrites config_backup.json without prompting
The -f flag suppresses interactive prompts and forces gunzip to overwrite existing destination files.
Real-World Workflows, Pipelines, and Shell Automation
Database Bulk Ingestion: Direct Streaming Without Intermediate Storage
Decompress and load massive SQL dumps directly into PostgreSQL or MySQL without filling disk space.
#!/usr/bin/env bash
set -euo pipefail
ARCHIVE="/backups/inventory_20261022.sql.gz"
TARGET_DB="production_inventory"
echo "Verifying archive integrity: $ARCHIVE"
gunzip -t "$ARCHIVE"
echo "Streaming data directly into database '$TARGET_DB'..."
gunzip -c "$ARCHIVE" | psql -U postgres -d "$TARGET_DB"
echo "Ingestion completed successfully."
# Illustrative output (reviewed against documentation)
Verifying archive integrity: /backups/inventory_20261022.sql.gz
Streaming data directly into database 'production_inventory'...
SET
CREATE TABLE
COPY 500000
Ingestion completed successfully.
Using gunzip -c avoids creating massive uncompressed SQL files on the filesystem, saving time and disk space.
Automated Archive Integrity Scanner: Nightly Backup Audit
Audit all compressed backups across an archive volume to detect bitrot or storage corruption.
#!/usr/bin/env bash
set -euo pipefail
BACKUP_DIR="/mnt/backups/daily"
echo "Auditing archive integrity in $BACKUP_DIR..."
FAILED_COUNT=0
while read -r gz_file; do
if ! gunzip -t "$gz_file" 2>/dev/null; then
echo "[CORRUPT] Archive checksum failed: $gz_file"
FAILED_COUNT=$((FAILED_COUNT + 1))
fi
done < <(find "$BACKUP_DIR" -type f -name "*.gz")
if [ "$FAILED_COUNT" -gt 0 ]; then
echo "[ALERT] $FAILED_COUNT corrupted archive(s) detected!"
exit 1
else
echo "[SUCCESS] All compressed archives passed CRC-32 integrity validation."
fi
# Illustrative output (reviewed against documentation)
Auditing archive integrity in /mnt/backups/daily...
[SUCCESS] All compressed archives passed CRC-32 integrity validation.
This workflow scans for silent disk corruption across archives using gunzip -t without extracting files.
Pipelines, Redirection, Quoting, and Shell Expansion
When embedding gunzip inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.
Permissions, Security Implications, and Safe Execution
gunzip DELETES the input .gz archive upon successful decompression by default. If you must retain your original archive files, always supply the -k (--keep) option.
Beware of zip bomb attacks when decompressing untrusted .gz files. A maliciously crafted 10MB gzip file can expand into over 100GB of uncompressed data, exhausting disk storage and crashing server processes. Always inspect gunzip -l file.gz before extracting.
gunzip preserves the original file permissions and ownership flags stored in the Gzip header. When extracting files created by another user, verify destination file permissions to prevent accidental privilege escalation.
GNU/Linux, BSD, and BusyBox Implementation Differences
The gunzip command is standard across all Linux distributions, macOS, and BSD systems. In almost all implementations, gunzip is simply a symlink or hardlink to gzip that sets decompression mode internally.
The -k (--keep) flag is a modern GNU extension. On legacy Unix systems where gunzip lacks -k, use gunzip -c file.gz > file to achieve identical, portable preservation.
Troubleshooting Common Errors and Diagnostics
gunzip: file.gz: not in gzip format
Root Cause: The file is not a valid Gzip archive, or the magic bytes (0x1F 0x8B) were corrupted or truncated.
Resolution: Inspect the file format with file file.gz to check the actual file type.
file file.gz
gunzip: file already exists; do you wish to overwrite (y or n)?
Root Cause: The uncompressed destination file already exists in the current directory, and gunzip is prompting for confirmation.
Resolution: Use the -f (--force) flag to overwrite without prompting.
gunzip -f file.gz
gunzip: unexpected end of file (CRC error)
Root Cause: The .gz file was truncated during download or network transfer, causing the CRC checksum to fail.
Resolution: Re-download the file and verify the remote file hash with sha256sum.
gunzip -t file.gz
Alternatives, Related Commands, and Related Tutorials
- gzip Command in Linux: Compress and Decompress Files with Deflate — Practical syntax, flags, and examples for
gzip. - How to Unzip a .gz File in Linux: gunzip, gzip -d, and zcat Guide — Practical syntax, flags, and examples for
gzip. - Tar Command in Linux: Create, Extract, and Manage Tape Archives — Practical syntax, flags, and examples for
tar. - tee Command in Linux: Split and Redirect Streams with Examples — Practical syntax, flags, and examples for
tee. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
/bin/gunzip is a hard link or symbolic link pointing directly to the gzip executable.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides



Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!