Quick take: The gzip command compresses a single file (replacing it with a .gz), and gunzip reverses it. Use -k to keep the original, -9 for maximum compression, and zcat to read a .gz without extracting.

Introduction

The gzip command is the standard single-file compressor in Linux, shrinking a file and giving it a .gz extension; gunzip (or gzip -d) decompresses it. Unlike tar, gzip works on one file at a time, which is why the two are combined as .tar.gz for archiving directories.

This guide covers compressing and decompressing, keeping the original, choosing a compression level, and reading gzipped files in place. For a deeper dive on gzip across web servers and Docker, see our dedicated gzip compression guide.

Syntax

The basic syntax of the gzip and gunzip command is:

gzip [OPTIONS] FILE
gunzip [OPTIONS] FILE.gz

Common Options and Parameters

The most useful options and parameters for the gzip and gunzip command:

OptionDescription
-kKeep the original file (do not delete it).
-dDecompress (same as gunzip).
-1 … -9Compression level (1 fastest, 9 smallest).
-rRecursively compress files in directories.
-lList compression stats for a .gz file.
-cWrite to stdout, keeping the input (for redirection).
-vVerbose — show the compression ratio.

Practical Examples

Real gzip and gunzip commands you can run today:

# Compress a file (replaces it with .gz)
gzip large.log
# Compress but keep the original
gzip -k large.log
# Maximum compression
gzip -9 large.log
# Decompress
gunzip large.log.gz
# Read a gzipped file without extracting
zcat large.log.gz | less
# Search inside a gzipped file
zgrep 'error' app.log.gz

Tips and Best Practices

  • By default gzip replaces the original with the .gz file — use -k to keep both.
  • Use the z-prefixed tools to work with .gz files directly: zcat, zless, and zgrep avoid manual extraction.
  • For compressing a whole directory, combine with tar: tar -czf archive.tar.gz folder/.

Final Thoughts

gzip and gunzip are the everyday single-file compressors of Linux, and the z-tools (zcat, zgrep) let you work with compressed files without unpacking them. Remember -k to keep the original and -9 for maximum compression, and pair gzip with tar for directories. For higher ratios, xz compresses smaller at the cost of speed.

FAQ: gzip and gunzip Command in Linux

How do I compress a file with gzip?+

Run gzip filename, which compresses it and replaces it with filename.gz. Add -k to keep the original file as well, or -9 for maximum compression.

How do I decompress a .gz file?+

Use gunzip file.gz or gzip -d file.gz. This restores the original file and removes the .gz version unless you used -k.

How do I keep the original file when compressing?+

Use the -k flag: gzip -k file keeps the original alongside the new .gz, instead of replacing it.

How do I read a gzipped file without extracting it?+

Use zcat file.gz to print it, zless file.gz to page through it, or zgrep pattern file.gz to search inside — all without unpacking.

What is the difference between gzip and tar.gz?+

gzip compresses a single file. tar.gz first bundles many files into one tar archive, then gzip-compresses it — which is why directories are distributed as .tar.gz.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support