zip and unzip in Linux CLI - Time & Space Complexity
When we use zip and unzip commands, we want to know how the time they take grows as the files get bigger or more numerous.
We ask: How does the work increase when we add more files or larger files?
Analyze the time complexity of the following code snippet.
zip archive.zip file1.txt file2.txt file3.txt
unzip archive.zip -d output_folder
This code compresses three files into one archive, then extracts them to a folder.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and compressing each file during
zip, and reading and decompressing each file duringunzip. - How many times: Once per file, processing all bytes inside each file.
As you add more files or bigger files, the time to zip or unzip grows roughly in proportion to the total size of all files combined.
| Input Size (total MB) | Approx. Operations |
|---|---|
| 10 | Processes about 10 MB of data |
| 100 | Processes about 100 MB of data |
| 1000 | Processes about 1000 MB (1 GB) of data |
Pattern observation: Doubling the total file size roughly doubles the work done.
Time Complexity: O(n)
This means the time grows linearly with the total size of the files being zipped or unzipped.
[X] Wrong: "Zipping many small files is always faster than one big file of the same size."
[OK] Correct: The total data size matters most, not how it is split. Overhead for many files is small compared to reading all bytes.
Understanding how compression tools scale helps you reason about performance in real tasks, showing you can think about how scripts behave with bigger data.
"What if we used a compression method that only compressed parts of files? How would the time complexity change?"
Practice
zip command do in Linux?Solution
Step 1: Understand the purpose of
Thezipzipcommand is used to compress files into one archive to save space or organize files.Step 2: Differentiate from other commands
unzipextracts files,lslists files, andrmdeletes files, so these are not correct.Final Answer:
It compresses one or more files into a single archive file. -> Option BQuick Check:
zip = compress files [OK]
- Confusing zip with unzip command
- Thinking zip lists files
- Assuming zip deletes files
archive.zip including all files in the folder docs recursively?Solution
Step 1: Identify the option for recursive zip
The-roption tellszipto include folders and their contents recursively.Step 2: Check command correctness
zip -r archive.zip docscorrectly creates the archive including all files insidedocs. Other options are incorrect or for unzip.Final Answer:
zip -r archive.zip docs -> Option AQuick Check:
Use -r to zip folders recursively [OK]
- Using unzip instead of zip to create archive
- Forgetting -r to include folders
- Using -x which excludes files
unzip -l archive.zip?Solution
Step 1: Understand the
The-loption in unzip-loption lists the files inside the zip archive without extracting them.Step 2: Differentiate from other actions
Extracting files uses unzip without -l, deleting is not done by unzip, and compressing is done by zip, not unzip.Final Answer:
It lists the contents of archive.zip without extracting. -> Option AQuick Check:
unzip -l lists archive contents [OK]
- Thinking unzip -l extracts files
- Confusing zip and unzip commands
- Assuming unzip deletes files
unzip archive.zip but get an error saying "cannot find or open archive.zip". What is the most likely cause?Solution
Step 1: Analyze the error message
"cannot find or open archive.zip" means the file is missing or path is wrong.Step 2: Check command and options
unzipis correct to extract files; no -r option is needed. Corruption would give a different error.Final Answer:
The archive.zip file does not exist in the current directory. -> Option CQuick Check:
File missing causes cannot find error [OK]
- Assuming unzip command is wrong
- Using -r option with unzip incorrectly
- Ignoring file location or name typos
backup.zip that includes all files and folders inside project, but exclude all .log files. Which command will do this correctly?Solution
Step 1: Use recursive zip with exclusion
The-roption includes folders recursively, and-x '*.log'excludes all .log files.Step 2: Validate command correctness
zip -r backup.zip project -x '*.log' uses correct syntax. zip backup.zip project/*.log only zips .log files, C uses invalid --exclude, and D uses unzip which cannot create archives.Final Answer:
zip -r backup.zip project -x '*.log' -> Option DQuick Check:
Use -r to recurse and -x to exclude files [OK]
- Using unzip instead of zip to create archive
- Wrong exclude syntax like --exclude
- Not using -r to include folders
Ready to prove it?
Pass the test and this pattern moves unknown
