gzip
Compress files using the DEFLATE algorithm
By CMD Script Team · 4 min read · Last updated
gzip [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-9 | Use maximum compression level (slowest, smallest output) |
-1 | Use fastest compression level (largest output, quickest) |
-k | Keep the original file instead of deleting it after compression |
-d | Decompress (equivalent to running gunzip) |
-r | Recursively compress files in directories |
-c | Write output to stdout, leaving the original file untouched |
-l | List compressed and uncompressed size, ratio, and name for .gz files |
-v | Verbose: 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 originalgzip -k file.txt— compress but keep the original file toogzip -d file.txt.gz— decompress back to file.txtgzip -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
tarto 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
.gzfile without decompressing it:gzip -l archive.tar.gz - Fine-tune the speed/size tradeoff for very large files where
-9is too slow:gzip -1 huge_dataset.csv
tar -cf - large_dir/ | gzip -9 > large_dir_backup.tar.gz
Common mistakes
- Expecting
gzipto compress a whole directory into one file the wayzipdoes —gziponly compresses single files; usetar -czffor directories. - Forgetting
-kand 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.gzfile is barely smaller — DEFLATE gets little benefit from data that's already compressed. - Confusing
gzip -d file.gzwith simply runninggunzip file.gz— they're equivalent, but mixing up the flag can lead to confusion about which mode you're in.
Tips
- Use
zcat file.gzto view a compressed file's contents without permanently decompressing it to disk. gzip -lshows 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
-kby default when compressing anything you might need to reference again quickly, since decompression, while fast, is an extra step you can avoid. - Prefer
tar -czfover 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
-9for 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
gzipdelete the original file? Yes, by default; use-kto keep it, or-cto write compressed output to stdout without touching the source at all. - Why can't
gzipcompress an entire directory into one archive?gziponly operates on individual files and has no archive format of its own; combining multiple files requirestarfirst, typically piped or invoked together astar -czf. - What's the difference between
gzip -d file.gzandgunzip file.gz? They're functionally identical —gunzipis essentiallygzip -d, both decompress the file and remove the.gzextension.
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.