>_cmd.script

gzip

Compress files using the DEFLATE algorithm

Compression

By CMD Script Team · 4 min read · Last updated

SYNTAX
gzip [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-9Use maximum compression level (slowest, smallest output)
-1Use fastest compression level (largest output, quickest)
-kKeep the original file instead of deleting it after compression
-dDecompress (equivalent to running gunzip)
-rRecursively compress files in directories
-cWrite output to stdout, leaving the original file untouched
-lList compressed and uncompressed size, ratio, and name for .gz files
-vVerbose: show the compression percentage and resulting filename

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

gzip compresses a single file using the DEFLATE algorithm, producing a .gz file and, by default, deleting the original. It's the classic Unix compression tool, distinct from archivers like tar in that it only compresses one file at a time and has no concept of bundling multiple files or directories — that's why tar and gzip are so often used together (tar -czf). Its companion tools gunzip, zcat, and zmore handle decompression and inspection of .gz files.

Beginner examples

  • gzip file.txt — compress file.txt into file.txt.gz, deleting the original
  • gzip -k file.txt — compress but keep the original file too
  • gzip -d file.txt.gz — decompress back to file.txt
  • gzip -9 file.txt — compress using maximum compression level
gzip -k access.log

Advanced examples

  • Compress every file in a directory tree recursively: gzip -r logs/
  • Combine with tar to compress an entire directory into one archive: tar -czf site_backup.tar.gz site/
  • Compress to stdout so you can pipe the result elsewhere without touching the source file: gzip -c database_dump.sql > database_dump.sql.gz
  • Check the compression ratio and sizes of an existing .gz file without decompressing it: gzip -l archive.tar.gz
  • Fine-tune the speed/size tradeoff for very large files where -9 is too slow: gzip -1 huge_dataset.csv
tar -cf - large_dir/ | gzip -9 > large_dir_backup.tar.gz

Common mistakes

  • Expecting gzip to compress a whole directory into one file the way zip does — gzip only compresses single files; use tar -czf for directories.
  • Forgetting -k and losing the uncompressed original when you actually wanted to keep both versions.
  • Re-compressing an already-compressed format (like a .jpg, .mp4, or .zip) and being surprised the .gz file is barely smaller — DEFLATE gets little benefit from data that's already compressed.
  • Confusing gzip -d file.gz with simply running gunzip file.gz — they're equivalent, but mixing up the flag can lead to confusion about which mode you're in.

Tips

  • Use zcat file.gz to view a compressed file's contents without permanently decompressing it to disk.
  • gzip -l shows the compression ratio, which is a quick sanity check on whether compressing a particular file type is even worthwhile.
  • For maximum compatibility with other tools expecting .tar.gz, always compress the tar archive as a whole (tar -czf) rather than gzip-ing many individual files inside a directory.

Best practices

  • Use -k by default when compressing anything you might need to reference again quickly, since decompression, while fast, is an extra step you can avoid.
  • Prefer tar -czf over manually gzip-ing many files when the goal is to package and compress a whole directory — it preserves structure and permissions in one archive.
  • Reserve -9 for cases where storage or bandwidth is the priority and CPU time is cheap; for frequently-compressed data (like rotated logs), the default level is usually a better time/space tradeoff.

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

  • Compressing rotated log files to save disk space: gzip /var/log/app.log.1.
  • Shrinking a database dump before transferring it off-server: gzip -9 database_backup.sql.
  • Building a compressed tarball of a project for distribution: tar -czf project-v1.0.tar.gz project/.

Common interview questions

  • Does gzip delete the original file? Yes, by default; use -k to keep it, or -c to write compressed output to stdout without touching the source at all.
  • Why can't gzip compress an entire directory into one archive? gzip only operates on individual files and has no archive format of its own; combining multiple files requires tar first, typically piped or invoked together as tar -czf.
  • What's the difference between gzip -d file.gz and gunzip file.gz? They're functionally identical — gunzip is essentially gzip -d, both decompress the file and remove the .gz extension.

Frequently Asked Questions

Does gzip keep the original file?

No, by default gzip replaces the original file with a .gz version and deletes the source. Use -k to keep the original file alongside the compressed one, or -c to write compressed output to stdout without touching the original at all.

How do I compress an entire directory?

gzip only compresses individual files, not directories, and doesn't bundle multiple files together. To compress a directory, first archive it with tar (tar -czf archive.tar.gz directory/), which uses gzip internally via the -z flag, or use gzip -r to compress each file inside a directory individually in place.

What's the difference between gzip -9 and the default compression level?

gzip defaults to level 6, a balance of speed and ratio. -9 maximizes compression at the cost of more CPU time and slower operation; -1 is fastest but produces larger output. The difference in final size between -6 and -9 is often small for typical text data.

How do I decompress a .gz file?

Use gunzip file.gz, or equivalently gzip -d file.gz. Both restore the original file and remove the .gz version, or use -k to keep the .gz file too.

Cheat sheet

Download a quick-reference cheat sheet for gzip.