gzip Compress and decompress files
Shrinks a single file using the gzip format, which adds a .gz ending. It does not bundle several files together, so use tar first when you need that. gunzip, or gzip -d, undoes the compression.
Synopsis
gzip [OPTION]... [FILE]...
gzip -d [OPTION]... [FILE.gz]...
Common options
| Option or form | What it does |
|---|---|
-k, --keep | Keep the original file. Without it, gzip deletes the input once it has made the .gz |
-d, --decompress | Decompress instead of compress. Same as running gunzip |
-c, --stdout | Write the result to standard output and leave the original file untouched |
-l, --list | Show compressed size, uncompressed size and ratio for .gz files |
-t, --test | Check that a .gz file is intact |
-v, --verbose | Show the name and compression ratio for each file |
-1 to -9 | Compression level. -1 (--fast) is quickest, -9 (--best) is smallest but slowest. The default is 6 |
-r, --recursive | Compress every file inside folders you name, one by one |
Examples
gzip -k app.log && ls app.log*
app.log
app.log.gz
gzip -c notes.txt > notes.txt.gz && ls notes.txt*
notes.txt
notes.txt.gz
gzip -k words.txt && gzip -dc words.txt.gz | sort -u
apple
fig
pear
gzip -k data.csv && gzip -t data.csv.gz
gzip -9 -k data.csv && ls data.csv*
data.csv
data.csv.gz
gzip app.log && ls app.log* && gzip -d app.log.gz && ls app.log*
app.log.gz
app.log
Notes
- By default gzip removes the original file after compressing it, and gzip -d removes the .gz file after decompressing. Use -k to keep it. -k exists in the gzip that ships with macOS. Very old gzip versions on other systems may not have it, so use -c with a redirect there.
- gzip works on one file at a time. To compress a folder, make a tar archive and compress that, for example tar -czf backup.tgz docs.
- gzip will skip a file if the result already exists, with a message such as "already exists -- skipping". Remove or rename the old file, or use -f to overwrite it.
- Compressed text shrinks a lot, but photos, videos and zip files are already compressed and gain little or nothing.
See also
Last reviewed 2026-10-07.