gzip and gunzip in Linux CLI - Time & Space Complexity
When using gzip and gunzip, it's important to understand how the time to compress or decompress files changes as file size grows.
We want to know how the work done by these commands grows when the input file gets bigger.
Analyze the time complexity of the following commands.
gzip largefile.txt
# Compresses the file largefile.txt into largefile.txt.gz
gunzip largefile.txt.gz
# Decompresses the file back to largefile.txt
These commands compress and decompress a file using gzip and gunzip.
Both gzip and gunzip process the file data in chunks, reading and writing repeatedly.
- Primary operation: Reading and processing each byte of the file.
- How many times: Once for each byte in the file, sequentially.
As the file size grows, the time to compress or decompress grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 KB | 10,240 operations |
| 100 KB | 102,400 operations |
| 1 MB (1024 KB) | 1,048,576 operations |
Pattern observation: Doubling the file size roughly doubles the work done.
Time Complexity: O(n)
This means the time to compress or decompress grows linearly with the file size.
[X] Wrong: "gzip compresses files instantly no matter the size."
[OK] Correct: Compression takes time proportional to file size because every byte must be processed.
Understanding how compression tools scale with file size helps you reason about performance in real tasks, showing you can think about efficiency beyond just writing commands.
"What if gzip used multi-threading to compress parts of the file in parallel? How would the time complexity change?"
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
