Skip to content

The gzip command

The gzip command compresses a file into FILE.gz and removes the original; gunzip…

The gzip command compresses a file into FILE.gz and removes the original; gunzip (or gzip -d) turns it back.

Taught in Intermediate, lesson 20

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

gzip [OPTION]... FILE...

Compress files (.gz). Compresses each file into FILE.gz and removes the original (keep it with -k). Text compresses well: a log file often shrinks to a tenth of its size. gunzip, or gzip -d, turns it back. gzip works on single files; to pack a whole folder, use tar -czf.

How to use gzip

Compress a file

gzip replaces the file with a smaller .gz file. Add -k to keep the original.

gzip -k server.log && ls -l server.log server.log.gz
-rw-r--r-- 1 learner learner 5152 Jan 12 09:00 server.log
-rw-r--r-- 1 learner learner  716 Jan 12 09:01 server.log.gz

Uncompress: gunzip

gzip notes.txt && gunzip notes.txt.gz && head -n 2 notes.txt
Note 1: call Sam
Note 2: renew the library books

Read a .gz file without unpacking it: zcat

gzip -k notes.txt && zcat notes.txt.gz | tail -n 2
Note 13: buy coffee
Note 14: water the plants

Options

-k, --keepkeep the original file
-d, --decompressuncompress, like gunzip
-c, --stdoutwrite the result to the screen (or a pipe) and leave the files alone
-l, --listshow the compressed and uncompressed sizes
-v, --verbosesay how much each file shrank

More examples

gzip -k notes.txt
Make notes.txt.gz and keep notes.txt
gzip -k server.log && gzip -l server.log.gz
How much it shrank
         compressed        uncompressed  ratio uncompressed_name
                716                5152 86.1% server.log

Questions about gzip

How do I gzip a whole folder?

gzip compresses single files. Pack the folder with tar and compress it in one step: tar -czf folder.tar.gz folder.

Learn it step by step

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.