The gzip & zip command in Linux

Two compression worlds coexist. gzip (with bzip2 and the stronger, modern zstd/xz) compresses a single file or stream — which is why it pairs with tar for directories, and why logs ship as .gz. zip bundles AND compresses, is native on Windows, and is the right format when the recipient is not a Linux person.

How gzip & zip works

gzip and zip share the same engine — DEFLATE, LZ77 back-references plus Huffman coding — wrapped in opposite containers. gzip wraps ONE stream with a header and CRC: perfect for pipes and for compressing a tar stream whole, so .tar.gz compresses across file boundaries. zip is an archive: each file compressed separately plus a central directory at the end, so any single file is listable and extractable without reading the rest — the property Windows Explorer depends on.

The containers dictate the trade-offs: tar.gz achieves better ratios on many small files but cannot random-access; zip random-accesses but pays a ratio penalty. And the engine generation matters more than either container: zstd delivers near-xz compression at speeds that make gzip look leisurely, which is why modern pipelines default to .tar.zst wherever both ends are under your control.

Syntax

gzip [-k -d] FILE   |   zip -r OUT.zip DIR   |   unzip FILE.zip

Common options

OptionWhat it does
gzip fileCompress to file.gz — and REMOVE the original (use -k to keep).
gzip -d / gunzipDecompress.
zcat / zless / zgrepRead and search .gz files without decompressing.
zip -r out.zip dir/Create a Windows-friendly archive of a directory.
unzip -l file.zipList contents before extracting.
zstd fileThe modern choice: better ratios AND much faster (file.zst).

How to use gzip & zip: examples

$ gzip -k huge.log

Compress, keeping the original. Without -k, gzip replaces the file — its most surprising default.

$ zgrep "ERROR" app.log.2.gz

Search inside rotated compressed logs directly.

$ zip -r project.zip project/ -x "project/node_modules/*"

A shareable archive, junk excluded.

$ unzip -l release.zip && unzip release.zip -d /opt/app/

Inspect, then extract to a chosen directory.

Real-world use cases for gzip & zip

Log rotation economics

Rotated logs compress 90%+ — gzip on rotation (logrotate's compress option) turns 50 GB of monthly logs into 4. zgrep and zless read them in place, so archives stay searchable without ever inflating.

Deliverables for mixed audiences

Shipping a release to clients on Windows: zip -r release-1.2.zip dist/ -x "*.map" opens with a double-click anywhere. Same tree for your own backups: tar with zstd, better ratio, faster, yours.

Pro tips and common mistakes

Frequently asked questions about gzip & zip

Why did gzip delete my file?

By design it replaces the original with the .gz. gzip -k keeps both — worth aliasing into your fingers.

gzip vs zip?

gzip compresses one stream (hence tar.gz for trees, better ratios); zip is archive+compression in one, universally opened on Windows/macOS. Audience decides.

What about xz and zstd?

xz: maximum compression, slow. zstd: near-xz ratios at gzip-beating speed — the modern default where you control both ends.

Related commands

find

Locate files anywhere in a directory tree by name, type, size, date — and act on them.

tar

Create and extract archives: tar.gz, tar.bz2 — with the flag combinations finally explained.

Want to build real fluency? The interactive course takes you through gzip & zip and every other essential command with guided, checked exercises.