0
0
Linux CLIscripting~5 mins

tar (create and extract archives) in Linux CLI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: tar (create and extract archives)
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of files grows, the time to create or extract grows roughly in direct proportion.

Input Size (n files)Approx. Operations
10About 10 file reads/writes
100About 100 file reads/writes
1000About 1000 file reads/writes

Pattern observation: Doubling the number of files roughly doubles the work done.

Final Time Complexity

Time Complexity: O(n)

This means the time grows linearly with the number of files processed.

Common Mistake

[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.

Interview Connect

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.

Self-Check

What if we compressed the archive while creating it (using -czf)? How would the time complexity change?