gunzip
Decompress .gz files
By CMD Script Team · 3 min read · Last updated
gunzip [OPTIONS] FILE.gz...Options
| Flag | Description |
|---|---|
-k | Keep the compressed original instead of deleting it after decompression |
-c | Write decompressed output to stdout, leaving the input file unchanged |
-f | Force decompression, overwriting existing output files or ignoring missing suffixes |
-v | Verbose: show filename and compression ratio for each file processed |
-l | List compressed size, uncompressed size, ratio, and name without decompressing |
-t | Test the compressed file's integrity without producing output |
-r | Recurse into directories, decompressing every .gz file found |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
gunzip decompresses files created by gzip, restoring the original file from its
.gz counterpart. It's the direct inverse of gzip — where gzip file produces
file.gz and removes file, gunzip file.gz produces file and removes file.gz.
Internally gunzip is just gzip invoked in decompress mode, so every gzip option
that makes sense in reverse (keep original, write to stdout, verbose, test) is available.
Beginner examples
gunzip file.gz— decompressfile.gzintofile, removing the.gzfilegunzip -k file.gz— decompress but keepfile.gzaround toogunzip -v file.gz— decompress with a verbose compression-ratio reportgunzip file1.gz file2.gz file3.gz— decompress several files in one command
gunzip -k access.log.gz
Advanced examples
- Decompress to stdout and feed straight into another command without touching disk:
gunzip -c dump.sql.gz | mysql mydb - Recursively decompress every
.gzfile in a directory tree:gunzip -r ./archived-logs/ - Verify a batch of downloaded compressed files aren't corrupted before processing:
gunzip -t *.gz - List size and compression ratio for a directory of compressed files without
decompressing any of them:
gunzip -l *.gz
gunzip -c nightly-backup.sql.gz | psql mydb
Common mistakes
- Running
gunzip file.gzand being surprised the.gzfile disappeared — that's expected behavior; use-kif you need to keep it. - Trying to
gunzipa.tar.gzand expecting the tarball to also be extracted — gunzip only handles the gzip layer, leaving you with a plain.tarfile that still needstar -xf. - Attempting to decompress a file that isn't actually gzip-formatted (e.g., renamed
.zipto.gz) and getting a cryptic "not in gzip format" error. - Running out of disk space because both the compressed and decompressed copies exist
simultaneously — check available space with
df -hfirst for large files.
Tips
- Use
gunzip -lto preview compression ratio and size before committing to a full decompress, especially for files of unknown origin. gunzip -tis a cheap way to validate downloaded.gzfiles in a CI pipeline before relying on them.- Remember
gzip -dandgunzipare interchangeable — pick whichever reads better in a given script for clarity.
Best practices
- Prefer
-kwhen decompressing files you might need to re-share or re-verify later, rather than relying on re-compressing afterward (which can produce a slightly different byte-for-byte file). - In automated pipelines, always check
gunzip's exit status (or pair with-tfirst) so a truncated download doesn't silently produce a partial, corrupted output file. - When only one command downstream needs the data, stream via
-cinstead of writing an intermediate decompressed file to disk.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Restoring a
.gz-compressed database dump before importing it:gunzip -c dump.sql.gz | mysql dbname. - Decompressing downloaded software distributions or datasets that are shipped as
.tar.gz(as the first stage beforetar -xf). - Batch-decompressing a directory of rotated, compressed log files for a log-analysis
tool that doesn't support reading
.gznatively.
Common interview questions
- What's the difference between gunzip and gzip -d? None — they're the same
operation;
gunzipis effectively an alias/wrapper forgzipin decompress mode. - How do you decompress a file without deleting the compressed original?
gunzip -k file.gz. - How would you fully extract a .tar.gz archive? Either
gunzip file.tar.gzfollowed bytar -xf file.tar, or more directly in one step withtar -xzf file.tar.gz.
Frequently Asked Questions
Does gunzip delete the .gz file after decompressing?
Yes, by default gunzip replaces file.gz with the decompressed file, deleting the .gz version — the mirror image of what gzip does. Use -k to keep the compressed original.
What's the difference between gunzip and gzip -d?
None functionally — gunzip is a separate executable (or symlink) that behaves exactly like `gzip -d`. Use whichever reads more clearly in context.
How do I decompress a .tar.gz in one step?
You don't need gunzip at all for that — `tar -xzf archive.tar.gz` decompresses and extracts in one command. Use gunzip directly only when you want the plain .tar file (or a single non-tar .gz file) on disk.
Can gunzip handle .zip files?
No. gunzip only understands the gzip (DEFLATE, .gz) format. For .zip archives use `unzip`.
Cheat sheet
Download a quick-reference cheat sheet for gunzip.