The gzip command in Linux is used to compress and decompress files quickly. It’s commonly used to reduce file size for storage, backups, and faster transfers.
⚡ Quick Overview
| Command | Purpose |
|---|---|
gzip file | Compress a file |
gunzip file.gz | Decompress a file |
gzip -k file | Keep original file |
gzip -d file.gz | Decompress (same as gunzip) |
📦 Compress Files
Basic Compression
gzip file.txt
👉 Replaces file.txt with file.txt.gz
Keep Original File
gzip -k file.txt
👉 Keeps both original and compressed file
Compress Multiple Files
gzip file1.txt file2.txt
📤 Decompress Files
Using gunzip
gunzip file.txt.gz
Using gzip
gzip -d file.txt.gz
📂 Compress Folders (Important)
gzip cannot compress folders directly. (more information on tar)
👉 Use tar + gzip:
tar -czvf archive.tar.gz folder/
🔍 View Contents Without Extracting
zcat file.txt.gz
👉 Prints file contents to terminal
⚙️ Useful Options
| Option | Description |
|---|---|
-k | Keep original file |
-d | Decompress |
-l | List file info |
-r | Recursive (compress directories) |
-1 to -9 | Compression level (fast → best) |
🚀 Real-World Examples
Compress logs
gzip logfile.log
Compress with max compression
gzip -9 largefile.txt
View compressed file
zcat logfile.log.gz
Combine with grep (more information on grep)
zcat logfile.gz | grep error
👉 Search inside compressed logs without extracting
⚠️ Common Mistakes
- ❌ Trying to compress folders directly
- ❌ Forgetting it deletes original file
- ❌ Using wrong tool (
zipvsgzip)
🧠 Pro Tips
- Use
-9for maximum compression - Use
zcatinstead of extracting large logs - Combine with
tarfor backups - Use
gzip -kto avoid losing originals
👍 Quick Summary
gzipcompresses files into.gzgunzipor-ddecompresses- Works on files, not folders
- Often used with
tar
🎯 Final Thoughts
The gzip command is fast, efficient, and widely used across Linux systems. Once combined with tar, it becomes one of the most powerful tools for backups and file management.