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-zontarmeans understanding whatgzipdoes. - It is fast — good compression in a fraction of a second, which is why it became the default over slower rivals.
zcatreads compressed files without unpacking them — a small superpower for inspecting logs and configs.
gzipreplaces the original file by default. If you just want to see the compressed size, keep the original with-kor pipe throughgzip -cto 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 filedeletesfileunless you pass-k. Use-kor-cwhen you want both copies. - Confusing
gzipwithtar.gz— a.tar.gzis a tar archive compressed by gzip; it is not a gzipped file you can read withzcatalone. Unpack the tar first. - Forgetting
gunziprestores the name — after decompressing, the original name is back; do not rungzipon the same name twice expecting a second copy. - Assuming
gzipcompresses directories — it cannot; compressing a directory meanstar-ing it first. - Using
gzipwherexzwould fit better — for maximum size savings on files you rarely touch,xzbeats gzip; see xz.
Related Pages
| Page | Relationship |
|---|---|
| tar | The bundler -z calls; the two are used together constantly |
| zip | The cross-platform format with built-in compression |
| xz | Higher-ratio compressor; smaller but slower than gzip |
| bzip2 | Older compressor; sits between gzip and xz |
gzip Cheatsheet
| Command | What it does |
|---|---|
gzip file | Compress to file.gz, removing the original |
gzip -k file | Compress but keep the original |
gzip -c file > out.gz | Compress to standard output (no deletion) |
gunzip file.gz | Decompress back to the original file |
gzip -d file.gz | Same as gunzip |
gzip -vk *.txt | Compress many files, verbosely |
gzip -t file.gz | Test archive integrity |
zcat file.gz | Print a compressed file without decompressing |
zcat file.gz | grep pattern | Search inside a compressed file |
ls -lh file file.gz | Compare original and compressed sizes |