0
0
Linux CLIscripting~5 mins

gzip and gunzip in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes files take up too much space or are slow to send over the internet. gzip helps by squeezing files smaller. gunzip lets you open those squeezed files back to normal.
When you want to save space on your computer by making files smaller.
When you need to send files faster over the internet by compressing them first.
When you receive a compressed file and want to open it to see the contents.
When you want to archive logs or backups in a smaller size.
When you want to reduce download time for files on a website.
Commands
This command squeezes the file named example.txt to make it smaller. It replaces the original file with a compressed version called example.txt.gz.
Terminal
gzip example.txt
Expected OutputExpected
No output (command runs silently)
This command checks that the compressed file example.txt.gz exists and shows its size.
Terminal
ls -l example.txt.gz
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 27 12:00 example.txt.gz
-l - Shows detailed file information including size.
This command opens the squeezed file example.txt.gz and restores it back to the original example.txt file.
Terminal
gunzip example.txt.gz
Expected OutputExpected
No output (command runs silently)
This command confirms that the original file example.txt is back and shows its size.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 user user 5678 Apr 27 12:01 example.txt
-l - Shows detailed file information including size.
Key Concept

If you remember nothing else from this pattern, remember: gzip makes files smaller by compressing them, and gunzip restores them back to normal.

Common Mistakes
Trying to gzip a file that is already compressed with .gz extension.
gzip will compress it again, which is usually unnecessary and wastes time.
Only gzip files that are not already compressed.
Using gunzip on a file that is not compressed or does not have a .gz extension.
gunzip will fail because it expects a compressed file format.
Only use gunzip on files ending with .gz that were compressed by gzip.
Expecting gzip to keep the original file after compression without flags.
By default, gzip deletes the original file after compressing it.
Use the -k flag with gzip to keep the original file if needed.
Summary
Use gzip filename to compress a file and save space.
Use gunzip filename.gz to decompress and restore the original file.
Check files with ls -l to see their sizes before and after compression.