0
0
Linux CLIscripting~5 mins

Why compression saves storage and bandwidth in Linux CLI - Why It Works

Choose your learning style9 modes available
Introduction
Compression reduces the size of files by removing unnecessary data patterns. This helps save space on your disk and makes files faster to send over the internet or network.
When you want to store large log files but have limited disk space.
When you need to send files over email or messaging apps with size limits.
When backing up data to external drives or cloud storage to save costs.
When transferring files between servers to reduce transfer time.
When archiving old projects to keep your workspace clean and efficient.
Commands
This command compresses the file example.txt using gzip, creating example.txt.gz which takes less space.
Terminal
gzip example.txt
Expected OutputExpected
No output (command runs silently)
-k - Keep the original file after compression
-v - Show compression details
This command lists the compressed file with human-readable size to see how much space was saved.
Terminal
ls -lh example.txt.gz
Expected OutputExpected
-rw-r--r-- 1 user user 1.2K Apr 27 12:00 example.txt.gz
-l - Show detailed file info
-h - Show sizes in human-readable format
This command decompresses the file back to its original form so you can use it again.
Terminal
gunzip example.txt.gz
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: compression shrinks files by removing repeated or unnecessary data, saving space and speeding up transfers.

Common Mistakes
Trying to compress already compressed files like .zip or .mp4
These files are already compressed and gzip won't reduce their size much, wasting time and CPU.
Avoid compressing files that are already compressed or use specialized tools for those formats.
Deleting original files before confirming compression success
If compression fails or file is corrupted, you lose the original data.
Use the -k flag to keep originals until you verify compressed files are good.
Not checking file sizes after compression
You might think compression saved space but it could be minimal or none.
Always check file sizes with ls -lh to confirm compression effectiveness.
Summary
Use gzip to compress files and save disk space and bandwidth.
Check compressed file sizes with ls -lh to see space savings.
Decompress files with gunzip when you need to use them again.