tar with compression (-z, -j, -J) in Linux CLI - Time & Space Complexity
When using tar with compression options, it is important to understand how the time to create or extract archives grows as the size of files increases.
We want to know how the execution time changes when compressing or decompressing larger amounts of data.
Analyze the time complexity of the following tar command with compression.
tar -czf archive.tar.gz folder/
# or
# tar -cjf archive.tar.bz2 folder/
# or
# tar -cJf archive.tar.xz folder/
This command creates a compressed archive of the folder using gzip (-z), bzip2 (-j), or xz (-J) compression.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each file's data and compressing it.
- How many times: Once for each byte of input data in the folder.
As the total size of files in the folder grows, the time to read and compress all data grows roughly in proportion.
| Input Size (n in 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 input size roughly doubles the time needed to compress.
Time Complexity: O(n)
This means the time to compress grows linearly with the total size of the input data.
[X] Wrong: "Compression time depends only on the number of files, not their size."
[OK] Correct: Compression works on the actual data size, so larger files take more time even if the file count is small.
Understanding how compression time scales helps you reason about script performance and system resource use in real tasks.
What if we used tar without compression? How would the time complexity change?
Practice
-z flag when used with the tar command?Solution
Step 1: Understand the
Thetarcompression flags-zflag tellstarto use gzip compression.Step 2: Match the flag to the compression type
-zis for gzip,-jfor bzip2, and-Jfor xz compression.Final Answer:
It compresses the archive using gzip. -> Option AQuick Check:
-zmeans gzip compression [OK]
- Confusing -z with -j or -J
- Thinking -z creates uncompressed archive
- Mixing compression types
backup.tar.bz2 from the directory data?Solution
Step 1: Identify the compression flag for bzip2
The-jflag is used for bzip2 compression.Step 2: Check the full command syntax
tar -cjf backup.tar.bz2 datacreates a bzip2 compressed archive namedbackup.tar.bz2fromdata.Final Answer:
tar -cjf backup.tar.bz2 data -> Option CQuick Check:
-jmeans bzip2 compression [OK]
- Using -z instead of -j for bzip2
- Forgetting the 'f' flag for filename
- Mixing compression flags and extensions
project contains files and subdirectories?tar -cJf project.tar.xz projectSolution
Step 1: Understand the
The-Jflag-Jflag tellstarto use xz compression.Step 2: Analyze the command output
The command creates a compressed archive namedproject.tar.xzusing xz compression from theprojectdirectory.Final Answer:
Creates an xz compressed tar archive named project.tar.xz -> Option BQuick Check:
-Jmeans xz compression [OK]
- Thinking -J is invalid
- Confusing -J with -z or -j
- Assuming no compression happens
tar -cjf archive.tar.gz folder but get an error. What is the likely cause?Solution
Step 1: Verify the flags and syntax
The command has all required flags:-cfor create,-jfor bzip2,-ffor file.Step 2: Identify the likely error source
tar will error if the sourcefolderdoes not exist, typically with "Cannot stat: No such file or directory".Final Answer:
The folder does not exist -> Option DQuick Check:
Check source exists before tar [OK]
- Assuming flag or extension issues
- Overlooking if folder exists
- Focusing on compression mismatch
logs directory using the best compression ratio available with tar. Which command should you use?Solution
Step 1: Identify compression types and their ratios
Gzip (-z) is fast but less compressed, bzip2 (-j) is better, and xz (-J) offers the best compression ratio.Step 2: Choose the best compression flag
Using-cJfcreates an xz compressed archive, which has the best compression ratio.Final Answer:
tar -cJf logs.tar.xz logs -> Option AQuick Check:
Best compression = xz = -J flag [OK]
- Choosing gzip or bzip2 for best compression
- Forgetting compression flag
- Using uncompressed tar by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
