tar (create and extract archives) in Linux CLI - Time & Space Complexity
When using tar to create or extract archives, it's helpful to understand how the time taken grows as the number of files changes.
We want to know how the work done by tar changes when the archive size grows.
Analyze the time complexity of the following tar commands.
tar -cf archive.tar folder/
tar -xf archive.tar
The first command creates an archive from a folder. The second extracts all files from the archive.
Look at what tar does repeatedly:
- Primary operation: Reading or writing each file in the folder or archive.
- How many times: Once for each file and directory inside the folder or archive.
As the number of files grows, the time to create or extract grows roughly in direct proportion.
| Input Size (n files) | Approx. Operations |
|---|---|
| 10 | About 10 file reads/writes |
| 100 | About 100 file reads/writes |
| 1000 | About 1000 file reads/writes |
Pattern observation: Doubling the number of files roughly doubles the work done.
Time Complexity: O(n)
This means the time grows linearly with the number of files processed.
[X] Wrong: "Creating or extracting archives takes the same time no matter how many files there are."
[OK] Correct: More files mean more reading and writing, so the time grows with the number of files.
Understanding how tools like tar scale with input size shows you can think about efficiency in real tasks, a useful skill in scripting and automation.
What if we compressed the archive while creating it (using -czf)? How would the time complexity change?
Practice
tar -cvf archive.tar folder/ command do?Solution
Step 1: Understand the flags used
The-cflag means create an archive,-vmeans verbose (show progress), and-fspecifies the archive file name.Step 2: Analyze the command effect
The command creates a new archive file namedarchive.tarcontaining the contents of thefolder/directory.Final Answer:
Creates a tar archive named archive.tar containing the folder directory -> Option CQuick Check:
tar -cvf = create archive [OK]
- Confusing -c (create) with -x (extract)
- Thinking -v compresses files
- Assuming it extracts files instead of creating
backup.tar.gz?Solution
Step 1: Identify extraction and compression flags
-xextracts,-vshows progress,-zhandles gzip compression, and-fspecifies the file.Step 2: Match flags to the command
To extract a gzip compressed archive, usetar -xvzf filename. tar -xvzf backup.tar.gz matches this syntax.Final Answer:
tar -xvzf backup.tar.gz -> Option AQuick Check:
Extract gzip archive = -xvzf [OK]
- Omitting -z when extracting gzip archives
- Using -c (create) instead of -x (extract)
- Confusing -v (verbose) as mandatory
tar -tvf archive.tar if archive.tar contains files file1.txt and file2.txt?Solution
Step 1: Understand the flags
-tlists contents of an archive,-vshows details, and-fspecifies the archive file.Step 2: Predict the command output
The command lists the files insidearchive.tarwith details, without extracting them.Final Answer:
Lists file1.txt and file2.txt with details without extracting -> Option DQuick Check:
-t lists contents, -v shows details [OK]
- Thinking it extracts files instead of listing
- Assuming -t and -v cannot be combined
- Confusing -t with -x
tar -cvzf archive.tar.gz folder/ but get an error. What is the likely cause?Solution
Step 1: Verify command syntax
The flags-c(create),-v(verbose),-z(gzip),-f archive.tar.gzare correct, with filename immediately after-f.Step 2: Determine error cause
The syntax is valid, so the likely cause is thatfolder/does not exist, causing a "Cannot stat: No such file or directory" error.Final Answer:
The folder/ directory does not exist -> Option BQuick Check:
tar create error = missing source directory [OK]
- Assuming flag order syntax error
- Overlooking if source directory exists
- Confusing with permission issues
project/ folder, but exclude all .log files inside it. Which command achieves this?Solution
Step 1: Understand exclusion and compression flags
The--exclude='*.log'option excludes files matching the pattern.-ccreates,-zcompresses with gzip,-vshows progress, and-fspecifies the archive filename.Step 2: Check correct flag order and placement
The exclude option must come before the folder argument. tar -czvf project.tar.gz --exclude='*.log' project/ correctly places--exclude='*.log'beforeproject/and uses correct flags.Final Answer:
tar -czvf project.tar.gz --exclude='*.log' project/ -> Option AQuick Check:
Exclude before folder, use -czvf for compressed archive [OK]
- Placing --exclude after folder argument
- Using -x (extract) instead of -c (create)
- Mixing flag order or misspelling exclude
Ready to prove it?
Pass the test and this pattern moves unknown
