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?"
Practice
755 mean for a file in Linux?Solution
Step 1: Understand numeric permission digits
Each digit represents permissions for user, group, and others respectively. 7 means read(4)+write(2)+execute(1)=7, 5 means read(4)+execute(1)=5.Step 2: Decode 755
User (7) has read, write, execute; group (5) and others (5) have read and execute only.Final Answer:
User can read, write, execute; group and others can read and execute -> Option AQuick Check:
755 = rwx r-x r-x [OK]
- Confusing 5 with write permission
- Thinking 7 means read+write only
- Mixing group and others permissions
script.sh to permission mode 755?Solution
Step 1: Identify the command to change permissions
The command to change file permissions ischmodfollowed by the numeric mode and filename.Step 2: Match the mode 755 with the command
To set mode 755, usechmod 755 script.sh.Final Answer:
chmod 755 script.sh -> Option DQuick Check:
chmod + numeric mode + filename = chmod 755 script.sh [OK]
- Using wrong numeric mode
- Swapping filename and mode
- Using chmod without mode
644, what will be the output of ls -l filename showing the permission part?Solution
Step 1: Decode numeric mode 644
6 = read(4)+write(2), 4 = read only, so user has read/write, group and others have read only.Step 2: Translate to symbolic permissions
Permissions are-rw-r--r--where '-' means regular file, 'rw-' user, 'r--' group, 'r--' others.Final Answer:
-rw-r--r-- -> Option AQuick Check:
644 = rw- r-- r-- [OK]
- Confusing 4 with execute permission
- Mixing user and group permissions
- Assuming 644 includes execute
chmod 755 myfile but ls -l myfile shows -rw-r--r--. What is the likely problem?Solution
Step 1: Understand chmod effect and output
chmod 755 should set execute permission, but output shows no execute bits.Step 2: Identify permission issue
If permissions did not change, likely user lacks rights to modify file mode (not owner or no sudo).Final Answer:
You do not have permission to change the file mode -> Option CQuick Check:
chmod no effect = permission denied [OK]
- Assuming syntax error
- Thinking file type blocks chmod
- Ignoring user permissions
Solution
Step 1: Define required permissions
Owner: executable + read + write (7), group and others: read only (4 each).Step 2: Construct numeric mode
Owner = 7 (rwx), group = 4 (r--), others = 4 (r--), so mode is 744.Final Answer:
744 -> Option BQuick Check:
744 = rwx r-- r-- [OK]
- Choosing 755 which allows execute for others
- Using 700 which denies read for others
- Confusing 644 with execute permission
Ready to prove it?
Pass the test and this pattern moves unknown
