What it does
gzip FILE replaces FILE with a smaller FILE.gz. gunzip FILE.gz (same as gzip -d) brings the original back. gzip is fast and everywhere; it is the default compression for logs and for .tar.gz archives.
gzip compresses one file at a time. To pack a folder, use tar -czf.
Tools to work with .gz files without unpacking them: zcat (show), zless (page), zgrep (search), zdiff (compare).
How to write it
gzip [OPTION]... [FILE]...
gunzip [OPTION]... [FILE]...
zcat [OPTION]... [FILE]...Options 19
Most used
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Compress a file
gzip access.log ls access.log* access.log.gzThe original is replaced by the .gz file.
2Decompress
gunzip access.log.gzSame as
gzip -d access.log.gz.3Keep the original
gzip -k database.sqlYou get database.sql and database.sql.gz.
4Read a compressed log without unpacking
zcat /var/log/messages-20261018.gz | tailNo temporary file, no extra space used.
5Search in compressed logs
zgrep -i "failed password" /var/log/secure-*.gzSearches old rotated logs directly.
6See the compression ratio
gzip -l backup.tar.gzShows compressed size, original size and ratio.
7Test a file after a transfer
gzip -t backup.sql.gz && echo OKFinds broken downloads or cut transfers.
8Compress a database dump on the fly
pg_dump appdb | gzip -9 > appdb-$(date +%F).sql.gzThe dump never touches the disk uncompressed.
9Restore a compressed dump directly
gunzip -c appdb-2026-10-25.sql.gz | psql appdb-cwrites to stdout, the file stays.10Compress all old files in a folder
find /srv/exports -name "*.csv" -mtime +7 -exec gzip {} \;Saves space for data you rarely read.
11Compress every file in a tree
gzip -r /tmp/old-reports/Each file is compressed alone; folders stay.
12Faster compression with all CPU cores
tar -cf - /data | pigz > data.tar.gzpigzis a parallel gzip; the output works with normal gunzip.
Nothing matches this filter.
Watch out
- gzip deletes the original by default. Use
-kor-cif you need it. - gzip does not pack folders into one file: use tar.
gzipon a file that is still being written (a live log) can lose data; let logrotate do it.
Tips to remember
- gzip = pack, gunzip = unpack, zcat/zgrep/zless = read packed files.
-kkeep,-cto stdout,-llist,-ttest.