Numeric permission mode (755, 644) in Linux CLI - Time & Space Complexity
When working with numeric permission modes like 755 or 644, it's helpful to understand how the system processes these permissions.
We want to see how the time to apply or check permissions changes as the number of files grows.
Analyze the time complexity of applying permissions to multiple files using a loop.
for file in *; do
chmod 755 "$file"
done
This script changes the permission mode to 755 for every file in the current directory.
Look at what repeats as the script runs.
- Primary operation: The
chmodcommand runs once for each file. - How many times: Exactly as many times as there are files in the directory.
As the number of files grows, the total work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 chmod commands |
| 100 | 100 chmod commands |
| 1000 | 1000 chmod commands |
Pattern observation: The work grows directly with the number of files. Double the files, double the commands.
Time Complexity: O(n)
This means the time to change permissions grows in a straight line with the number of files.
[X] Wrong: "Changing permissions on many files happens instantly regardless of how many files there are."
[OK] Correct: Each file needs its own permission change, so more files mean more work and more time.
Understanding how commands scale with input size helps you explain script efficiency clearly and confidently in real situations.
"What if we used a single command to change permissions recursively on a directory instead of looping over files? How would the time complexity change?"