Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gzip Command Guide for Compressing Single Files

gzip is the classic single-file compressor — it shrinks a file to roughly a third of its size and gives it a .gz extension, and gunzip/zcat reverse the process.


What is gzip, and Why Use It?

gzip compresses one file at a time using the DEFLATE algorithm, the same method tar -z and .zip use under the hood. When you run it, it replaces the original file with a smaller .gz file — unless you keep the original with -k. Because it works on single files only, it is almost always paired with tar (which bundles many files first): tar -czf is “bundle with tar, compress with gzip”.

Why Learn gzip?

  • It is the default compression on Linux — source releases, web downloads, and many package files arrive as .gz.
  • It pairs with tar — understanding -z on tar means understanding what gzip does.
  • It is fast — good compression in a fraction of a second, which is why it became the default over slower rivals.
  • zcat reads compressed files without unpacking them — a small superpower for inspecting logs and configs.

gzip replaces the original file by default. If you just want to see the compressed size, keep the original with -k or pipe through gzip -c to write to standard output instead.


1. Compress a File

gzip notes.txt

This creates notes.txt.gz and removes notes.txt. To keep the original around:

gzip -k notes.txt
  • -k — keep the input file.

To see how well it compressed:

ls -lh notes.txt notes.txt.gz

2. Decompress a File

gunzip and gzip -d do the same thing:

gunzip notes.txt.gz
gzip -d notes.txt.gz

Both restore notes.txt and remove the .gz file. The file comes back with its original content; the original name is recovered from the .gz suffix.

3. Compress Without Deleting (Output to Standard Output)

To compress into a new file without touching the original:

gzip -c notes.txt > notes-backup.gz
  • -c — write to standard output (the console/pipe), not to a file.

This is the pattern to remember: -c turns gzip into a filter you can redirect, the same way cat works.

4. Read a Compressed File Without Decompressing

zcat (and gzip -c) shows the contents of a .gz file directly:

zcat access.log.gz

This is how you peek inside a compressed log without unpacking it:

zcat /var/log/syslog.2.gz | grep "error"

Pipe it into anything that reads standard input — grep, less, head, wc:

zcat notes.txt.gz | head -5
zcat access.log.gz | wc -l

5. Compress Many Files at Once

gzip accepts multiple filenames and compresses each one independently:

gzip *.txt

Every .txt in the directory becomes its own .gz. To list what it would do without doing it, add -v:

gzip -vk *.txt

6. Test Whether a File Is Valid

If you suspect a download was corrupted:

gzip -t backup.tar.gz
  • -t — test the integrity of the compressed data.

No output means the file is fine.

7. The Relationship With tar

gzip and tar are designed to work together. When you see tar -czf, the -z is telling tar to pipe the archive through gzip after packing it:

# Same result, written two ways:
tar -czf project.tar.gz project/
tar -cf - project/ | gzip > project.tar.gz

The first form is the one you will actually type. You will rarely run gzip alone on a whole project — only on single files like logs or config exports.


Common Pitfalls

  • Losing the original — gzip file deletes file unless you pass -k. Use -k or -c when you want both copies.
  • Confusing gzip with tar.gz — a .tar.gz is a tar archive compressed by gzip; it is not a gzipped file you can read with zcat alone. Unpack the tar first.
  • Forgetting gunzip restores the name — after decompressing, the original name is back; do not run gzip on the same name twice expecting a second copy.
  • Assuming gzip compresses directories — it cannot; compressing a directory means tar-ing it first.
  • Using gzip where xz would fit better — for maximum size savings on files you rarely touch, xz beats gzip; see xz.
PageRelationship
tarThe bundler -z calls; the two are used together constantly
zipThe cross-platform format with built-in compression
xzHigher-ratio compressor; smaller but slower than gzip
bzip2Older compressor; sits between gzip and xz

gzip Cheatsheet

CommandWhat it does
gzip fileCompress to file.gz, removing the original
gzip -k fileCompress but keep the original
gzip -c file > out.gzCompress to standard output (no deletion)
gunzip file.gzDecompress back to the original file
gzip -d file.gzSame as gunzip
gzip -vk *.txtCompress many files, verbosely
gzip -t file.gzTest archive integrity
zcat file.gzPrint a compressed file without decompressing
zcat file.gz | grep patternSearch inside a compressed file
ls -lh file file.gzCompare original and compressed sizes