Compress and Decompress Files Using gzip and gunzip
📖 Scenario: You have a text file with important notes that you want to save space on your computer. Compressing the file will make it smaller. Later, you want to open the file again by decompressing it.
🎯 Goal: Learn how to compress a file using gzip and decompress it back using gunzip in the Linux command line.
📋 What You'll Learn
Create a text file named notes.txt with specific content.
Use gzip to compress the notes.txt file.
Use gunzip to decompress the compressed file.
Display the content of the decompressed file.
💡 Why This Matters
🌍 Real World
Compressing files helps save disk space and makes it easier to transfer files over the internet.
💼 Career
Knowing how to compress and decompress files is essential for system administrators, developers, and anyone working with servers or large data files.
Progress0 / 4 steps
1
Create the text file notes.txt
Create a file named notes.txt with the exact content: Hello, this is a test file for gzip compression.
Linux CLI
Hint
Use the echo command with redirection > to create the file.
2
Compress the file using gzip
Use the gzip command to compress the file notes.txt. This will create a compressed file named notes.txt.gz.
Linux CLI
Hint
Simply run gzip notes.txt to compress the file.
3
Decompress the file using gunzip
Use the gunzip command to decompress the file notes.txt.gz. This will restore the original notes.txt file.
Linux CLI
Hint
Run gunzip notes.txt.gz to decompress the file.
4
Display the content of the decompressed file
Use the cat command to display the content of the decompressed file notes.txt.
Linux CLI
Hint
Use cat notes.txt to show the file content.
Practice
(1/5)
1. What does the gzip command do in Linux?
easy
A. Lists files in a directory
B. Deletes a file permanently
C. Compresses a file and replaces the original with a .gz file
D. Creates a backup copy of a file
Solution
Step 1: Understand gzip purpose
The gzip command 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 C
Quick Check:
gzip compresses files = D [OK]
Hint: gzip compresses and replaces original file by default [OK]
Common Mistakes:
Thinking gzip deletes files without compression
Confusing gzip with backup commands
Assuming gzip lists files
2. Which of the following is the correct syntax to compress a file named data.txt but keep the original file?
easy
A. gzip data.txt
B. gzip -d data.txt
C. gunzip -k data.txt
D. gzip -k data.txt
Solution
Step 1: Identify option to keep original
The -k option tells gzip to keep the original file after compression.
Step 2: Match correct command
gzip -k data.txt compresses and keeps the original file.
Final Answer:
gzip -k data.txt -> Option D
Quick Check:
Use -k to keep original file [OK]
Hint: Use -k with gzip to keep original file [OK]
Common Mistakes:
Using gunzip instead of gzip for compression
Using -d which is for decompression
Not using -k and losing original file
3. What will be the output of the following commands?
gzip -k report.txt
gunzip report.txt.gz
medium
A. Only report.txt exists after commands
B. Both report.txt and report.txt.gz exist after commands
C. Original report.txt deleted, only report.txt.gz remains
D. Error because gunzip cannot decompress .gz files
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 A
Quick Check:
gzip -k keeps original, gunzip restores and removes .gz [OK]
Hint: gunzip removes .gz file after decompression [OK]
Common Mistakes:
Assuming gzip deletes original with -k option
Thinking gunzip keeps .gz file after decompression
Confusing gzip and gunzip roles
4. You run gunzip file.txt but get an error saying "not in gzip format." What is the likely problem?
medium
A. gunzip command is misspelled
B. file.txt is not a compressed .gz file
C. file.txt is already decompressed
D. You need to use gzip instead of gunzip
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 B
Quick Check:
gunzip needs .gz files [OK]
Hint: gunzip only works on gzip-compressed (.gz) files [OK]
Common Mistakes:
Trying to decompress uncompressed files
Confusing gzip and gunzip commands
Ignoring file extensions
5. You have multiple log files: 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?
hard
A. gzip -k log*.txt
B. gzip log*.txt
C. gunzip -k log*.txt
D. gzip -d log*.txt
Solution
Step 1: Identify command to compress multiple files
gzip compresses files; log*.txt matches all log files.
Step 2: Ensure original files are kept
The -k option keeps original files after compression.
Final Answer:
gzip -k log*.txt -> Option A
Quick Check:
gzip -k compresses and keeps originals [OK]
Hint: Use gzip -k with wildcard to keep originals [OK]
Common Mistakes:
Using gunzip instead of gzip for compression
Not using -k and losing original files
Using -d which decompresses files
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.