Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

gzip, gunzip, zcat

Compress files to .gz (gzip), unpack them (gunzip), or read them without unpacking (zcat).

Level
RHCSA exam
Group
Archive and compress
Needs root
No
Package
gzip
Also called
gunzip, zcat, zgrep, zless

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

-d, --decompress
Decompress (same as gunzip).
-k, --keep
Keep the original file.
-c, --stdout
Write to the screen (stdout); keep the files.
-l, --list
Show the compressed and uncompressed size and the ratio.
-r, --recursive
Compress every file in the folders.
-t, --test
Test the compressed file (is it complete and not damaged?).
-v, --verbose
Show the name and the ratio of each file.
-q, --quiet
No warnings.
-f, --force
Overwrite existing files; also compress links and files with more names.
-1, --fast
Fastest compression (bigger file).
-9, --best
Best compression (slower). The default is -6.
-n, --no-name
Do not save (or restore) the original name and time.
-N, --name
Save (or restore) the original name and time.
-S, --suffix=suf
Use another ending instead of .gz.
--rsyncable
Make the output friendly for rsync (small changes = small transfers).
--synchronous
Write safely to disk (slower).
-h, --help
Show help.
-L, --license
Show the license.
-V, --version
Show the version.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Compress a file

    gzip access.log
    ls access.log*
    access.log.gz

    The original is replaced by the .gz file.

  2. 2Decompress

    gunzip access.log.gz

    Same as gzip -d access.log.gz.

  3. 3Keep the original

    gzip -k database.sql

    You get database.sql and database.sql.gz.

  4. 4Read a compressed log without unpacking

    zcat /var/log/messages-20261018.gz | tail

    No temporary file, no extra space used.

  5. 5Search in compressed logs

    zgrep -i "failed password" /var/log/secure-*.gz

    Searches old rotated logs directly.

  6. 6See the compression ratio

    gzip -l backup.tar.gz

    Shows compressed size, original size and ratio.

  7. 7Test a file after a transfer

    gzip -t backup.sql.gz && echo OK

    Finds broken downloads or cut transfers.

  8. 8Compress a database dump on the fly

    pg_dump appdb | gzip -9 > appdb-$(date +%F).sql.gz

    The dump never touches the disk uncompressed.

  9. 9Restore a compressed dump directly

    gunzip -c appdb-2026-10-25.sql.gz | psql appdb

    -c writes to stdout, the file stays.

  10. 10Compress all old files in a folder

    find /srv/exports -name "*.csv" -mtime +7 -exec gzip {} \;

    Saves space for data you rarely read.

  11. 11Compress every file in a tree

    gzip -r /tmp/old-reports/

    Each file is compressed alone; folders stay.

  12. 12Faster compression with all CPU cores

    tar -cf - /data | pigz > data.tar.gz

    pigz is a parallel gzip; the output works with normal gunzip.

Watch out

  • gzip deletes the original by default. Use -k or -c if you need it.
  • gzip does not pack folders into one file: use tar.
  • gzip on 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.
  • -k keep, -c to stdout, -l list, -t test.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man gzip