gzip Command: How to Compress & Decompress Files in Linux

Updated

Compressing files in a terminal window

gzip file deletes file and replaces it with file.gz — there is no confirmation prompt. If you need the original kept, use gzip -k file (keep) or gzip -c file > file.gz (write to stdout, original untouched). For compressing a whole directory, pair it with tar: tar -czf archive.tar.gz dir/. For a folder going to someone else a .zip is the usual choice, and I cover that command and what zip drops in my Mac Terminal zip guide.

The replace-by-default behavior, demonstrated

$ printf 'sample text for gzip demo, repeated repeated repeated repeated.\n' > notes.txt
$ wc -c notes.txt
      64 notes.txt
$ gzip notes.txt
$ ls notes.txt.gz notes.txt
ls: notes.txt: No such file or directory
notes.txt.gz
$ wc -c notes.txt.gz
      69 notes.txt.gz

notes.txt is gone. This particular file actually got bigger — 64 bytes in, 69 out — because gzip’s header and checksum overhead outweighs any savings on 64 bytes of low-redundancy text; compression only pays off once the redundant content outweighs the framing cost. gzip -k avoids the data-loss risk entirely by leaving the source file in place:

$ printf 'second demo file, repeated repeated repeated repeated repeated.\n' > report.txt
$ gzip -k report.txt
$ wc -c report.txt report.txt.gz
      64 report.txt
      61 report.txt.gz
     125 total

zcat is not what you think it is on macOS

On Linux, zcat file.gz is the standard way to read a gzip file without writing a decompressed copy to disk. On this Mac, it fails:

$ zcat report.txt.gz
zcat: can't stat: report.txt.gz (report.txt.gz.Z): No such file or directory

macOS’s /usr/bin/zcat is the old compress-family tool and expects a .Z suffix, not .gz. The gzip-compatible reader here is gzcat, or the portable gzip -dc, which works on both platforms:

$ gzcat report.txt.gz
second demo file, repeated repeated repeated repeated repeated.
$ gzip -dc report.txt.gz
second demo file, repeated repeated repeated repeated repeated.

zless, for paging through a compressed file, is unaffected and works normally on both. If a script needs to run on both a Mac and a Linux box, write gzip -dc, not zcat.

Compressing already-compressed data makes it bigger

gzip refuses to re-add its own suffix (gzip report.txt.gz just errors “already has .gz suffix”), so to show what compressing compressed bytes actually does, copy the payload to a plain name first:

$ cp report.txt.gz already.dat
$ wc -c already.dat
      61 already.dat
$ gzip already.dat
$ wc -c already.dat.gz
      94 already.dat.gz
$ gzip -l report.txt.gz
  compressed uncompressed  ratio uncompressed_name
          61           64   6.2% report.txt

61 bytes became 94 — DEFLATE output is close to random from an outside compressor’s perspective, so a second pass finds no redundancy to remove and just adds another header and checksum on top. The same logic applies to gzipping a JPEG, an MP4, or a ZIP: you’ll spend CPU time to make the file larger, not smaller. gzip -l reports the ratio directly without needing a second command.

For the actual gzip-vs-bzip2-vs-xz-vs-zstd speed and ratio numbers on real data, see the measured benchmark on extracting .tar.bz2 files — it’s not repeated here to avoid two pages claiming the same result.

/verify/gzip-compress-decompress.sh re-runs every check above and reports PASS/FAIL/SKIP.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.