>_cmd.script

gunzip

Decompress .gz files

Compression

By CMD Script Team · 3 min read · Last updated

SYNTAX
gunzip [OPTIONS] FILE.gz...

Options

Command options and flags
FlagDescription
-kKeep the compressed original instead of deleting it after decompression
-cWrite decompressed output to stdout, leaving the input file unchanged
-fForce decompression, overwriting existing output files or ignoring missing suffixes
-vVerbose: show filename and compression ratio for each file processed
-lList compressed size, uncompressed size, ratio, and name without decompressing
-tTest the compressed file's integrity without producing output
-rRecurse 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 — decompress file.gz into file, removing the .gz file
  • gunzip -k file.gz — decompress but keep file.gz around too
  • gunzip -v file.gz — decompress with a verbose compression-ratio report
  • gunzip 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 .gz file 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.gz and being surprised the .gz file disappeared — that's expected behavior; use -k if you need to keep it.
  • Trying to gunzip a .tar.gz and expecting the tarball to also be extracted — gunzip only handles the gzip layer, leaving you with a plain .tar file that still needs tar -xf.
  • Attempting to decompress a file that isn't actually gzip-formatted (e.g., renamed .zip to .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 -h first for large files.

Tips

  • Use gunzip -l to preview compression ratio and size before committing to a full decompress, especially for files of unknown origin.
  • gunzip -t is a cheap way to validate downloaded .gz files in a CI pipeline before relying on them.
  • Remember gzip -d and gunzip are interchangeable — pick whichever reads better in a given script for clarity.

Best practices

  • Prefer -k when 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 -t first) so a truncated download doesn't silently produce a partial, corrupted output file.
  • When only one command downstream needs the data, stream via -c instead 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 before tar -xf).
  • Batch-decompressing a directory of rotated, compressed log files for a log-analysis tool that doesn't support reading .gz natively.

Common interview questions

  • What's the difference between gunzip and gzip -d? None — they're the same operation; gunzip is effectively an alias/wrapper for gzip in 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.gz followed by tar -xf file.tar, or more directly in one step with tar -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.