chmod (change permissions) in Linux CLI - Time & Space Complexity
When using chmod to change file permissions, it's helpful to understand how the time it takes grows as you change more files.
We want to know: how does the work increase when we apply chmod to many files?
Analyze the time complexity of the following code snippet.
for file in /path/to/files/*; do
chmod 644 "$file"
done
This script changes the permissions of every file in a folder to 644.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running
chmodon each file. - How many times: Once for every file in the folder.
As the number of files grows, the total work grows too because chmod runs once per file.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 10 chmod commands |
| 100 files | 100 chmod commands |
| 1000 files | 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 finish grows in a straight line with the number of files you change.
[X] Wrong: "Running chmod on many files takes the same time as on one file."
[OK] Correct: Each file needs its own chmod command, so more files mean more work and more time.
Understanding how commands like chmod scale helps you write scripts that run efficiently when handling many files.
"What if we used chmod with the -R option to change permissions recursively? How would the time complexity change?"
Practice
chmod 755 filename command do in Linux?Solution
Step 1: Understand numeric permission 755
The digits 7, 5, 5 represent permissions for owner, group, and others respectively. 7 means read(4)+write(2)+execute(1)=7, 5 means read(4)+execute(1)=5.Step 2: Apply permissions to file
Owner gets full permissions (rwx), group and others get read and execute (r-x). This matches the description in Sets read, write, execute for owner; read and execute for group and others.Final Answer:
Sets read, write, execute for owner; read and execute for group and others -> Option CQuick Check:
chmod 755 = rwxr-xr-x [OK]
- Confusing numeric codes with letters
- Thinking 755 removes permissions
- Assuming chmod changes ownership
script.sh?Solution
Step 1: Recall chmod symbolic syntax
The correct format ischmod [who][operator][permission] filename. Here, 'u' means user, '+' adds permission, 'x' is execute.Step 2: Match syntax to options
chmod u+x script.sh matches the correct syntax:chmod u+x script.sh. Other options have incorrect order or misplaced arguments.Final Answer:
chmod u+x script.sh -> Option DQuick Check:
chmod u+x filename adds execute for user [OK]
- Placing permissions before user
- Putting filename before options
- Using spaces inside symbolic mode
ls -l file.txt after running chmod 640 file.txt on a file owned by user 'alice' and group 'staff'?Solution
Step 1: Decode chmod 640 permissions
6 = read(4)+write(2) for owner, 4 = read(4) for group, 0 = no permissions for others.Step 2: Translate to ls -l format
Owner: rw-, Group: r--, Others: ---. So the permission string is '-rw-r-----'. The rest shows ownership and file info.Final Answer:
-rw-r----- 1 alice staff 0 date file.txt -> Option AQuick Check:
chmod 640 = rw-r----- [OK]
- Mixing group and others permissions
- Assuming execute permission is set
- Confusing numeric values
chmod 777 myfile but ls -l myfile still shows -rw-r--r--. What is the most likely reason?Solution
Step 1: Understand chmod effect and file system state
chmod 777 should set full permissions. If permissions don't change, it may be due to file system restrictions like read-only mount.Step 2: Evaluate other options
Running as root is not always required for own files. Syntax is correct. File type does not prevent permission changes. Read-only file system blocks changes.Final Answer:
The file system is mounted as read-only -> Option BQuick Check:
Read-only FS blocks chmod changes [OK]
- Assuming sudo always needed
- Blaming syntax without checking
- Ignoring file system mount options
data.txt. Which command achieves this?Solution
Step 1: Translate desired permissions to numeric code
Owner: read+write = 6, Group: read only = 4, Others: no permissions = 0. So numeric code is 640.Step 2: Match numeric code to options
chmod 640 data.txt ischmod 640 data.txt, which matches the desired permissions exactly.Final Answer:
chmod 640 data.txt -> Option AQuick Check:
Owner=rw, Group=r, Others=none = 640 [OK]
- Using 750 which gives execute permission
- Using 664 which allows others read
- Using 600 which denies group read
Ready to prove it?
Pass the test and this pattern moves unknown
