Discover how a simple command can save you hours of tedious file handling!
Why gzip and gunzip in Linux CLI? - Purpose & Use Cases
Imagine you have hundreds of large files on your computer. You want to send them to a friend or save space on your hard drive. Doing this by copying each file one by one or manually compressing them with complicated tools feels like a never-ending chore.
Manually compressing files without simple commands is slow and confusing. You might forget steps, use too much space, or send incomplete files. It's easy to make mistakes and waste time, especially when handling many files.
Using gzip and gunzip commands lets you quickly compress and decompress files with simple commands. They save space and make sharing files faster and easier, all with just a few keystrokes.
zip files.zip file1 file2 file3 cp files.zip backup/ unzip files.zip
gzip file1 gzip file2 gunzip file1.gz
You can save disk space and transfer files faster by compressing and decompressing them effortlessly.
When sending photos or documents by email, compressing them with gzip reduces file size so they upload quickly and your friend can unzip them easily.
Manual file handling is slow and error-prone.
gzip and gunzip simplify compressing and decompressing files.
They help save space and speed up file sharing.
Practice
gzip command do in Linux?Solution
Step 1: Understand gzip purpose
Thegzipcommand compresses files to save disk space.Step 2: Effect on original file
It replaces the original file with a compressed file ending in.gz.Final Answer:
Compresses a file and replaces the original with a .gz file -> Option CQuick Check:
gzip compresses files = D [OK]
- Thinking gzip deletes files without compression
- Confusing gzip with backup commands
- Assuming gzip lists files
data.txt but keep the original file?Solution
Step 1: Identify option to keep original
The-koption tells gzip to keep the original file after compression.Step 2: Match correct command
gzip -k data.txtcompresses and keeps the original file.Final Answer:
gzip -k data.txt -> Option DQuick Check:
Use -k to keep original file [OK]
- Using gunzip instead of gzip for compression
- Using -d which is for decompression
- Not using -k and losing original file
gzip -k report.txt gunzip report.txt.gz
Solution
Step 1: Analyze gzip -k report.txt
This compresses report.txt to report.txt.gz and keeps the original report.txt file.Step 2: Analyze gunzip report.txt.gz
This decompresses report.txt.gz back to report.txt and deletes the .gz file.Final Answer:
Only report.txt exists after commands -> Option AQuick Check:
gzip -k keeps original, gunzip restores and removes .gz [OK]
- Assuming gzip deletes original with -k option
- Thinking gunzip keeps .gz file after decompression
- Confusing gzip and gunzip roles
gunzip file.txt but get an error saying "not in gzip format." What is the likely problem?Solution
Step 1: Understand gunzip input requirement
gunzip expects a file compressed with gzip, usually ending with .gz.Step 2: Analyze error message
"not in gzip format" means the file is not compressed or not a .gz file.Final Answer:
file.txt is not a compressed .gz file -> Option BQuick Check:
gunzip needs .gz files [OK]
- Trying to decompress uncompressed files
- Confusing gzip and gunzip commands
- Ignoring file extensions
log1.txt, log2.txt, and log3.txt. You want to compress all of them but keep the original files. Which command will do this correctly?Solution
Step 1: Identify command to compress multiple files
gzipcompresses files;log*.txtmatches all log files.Step 2: Ensure original files are kept
The-koption keeps original files after compression.Final Answer:
gzip -k log*.txt -> Option AQuick Check:
gzip -k compresses and keeps originals [OK]
- Using gunzip instead of gzip for compression
- Not using -k and losing original files
- Using -d which decompresses files
Ready to prove it?
Pass the test and this pattern moves unknown
