Permission types (read, write, execute) in Linux CLI - Time & Space Complexity
When working with file permissions in Linux, it helps to understand how checking permissions scales as you look at more files or users.
We want to see how the time to check permissions grows as the number of files or users increases.
Analyze the time complexity of the following code snippet.
for file in /some/directory/*; do
if [ -r "$file" ]; then
echo "$file is readable"
fi
if [ -w "$file" ]; then
echo "$file is writable"
fi
if [ -x "$file" ]; then
echo "$file is executable"
fi
done
This script checks read, write, and execute permissions for each file in a directory.
- Primary operation: Looping over each file and checking three permission types.
- How many times: Once per file in the directory.
As the number of files grows, the script checks permissions for each file, doing three checks per file.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 permission checks |
| 100 | 300 permission checks |
| 1000 | 3000 permission checks |
Pattern observation: The number of operations grows directly with the number of files.
Time Complexity: O(n)
This means the time to check permissions grows linearly as the number of files increases.
[X] Wrong: "Checking permissions is instant and does not depend on the number of files."
[OK] Correct: Each file requires separate permission checks, so more files mean more work.
Understanding how permission checks scale helps you write efficient scripts and shows you can think about performance in real tasks.
"What if we checked permissions only for files modified in the last day? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of read permission
Read permission allows a user to open and view the contents of a file without changing it.Step 2: Compare with other permissions
Write permission allows changing the file, execute permission allows running the file as a program, and delete is not a standard permission type.Final Answer:
Read permission -> Option AQuick Check:
Read = open/view file [OK]
- Confusing write with read permission
- Thinking execute lets you open files
- Assuming delete is a permission type
Solution
Step 1: Recall symbolic letters for permissions
In Linux, 'r' stands for read, 'w' for write, and 'x' for execute permission.Step 2: Identify the write permission symbol
The letter 'w' is used to represent write permission.Final Answer:
w -> Option AQuick Check:
Write permission symbol = w [OK]
- Mixing 'r' and 'w' symbols
- Using 'e' which is not a permission symbol
- Confusing execute 'x' with write
ls -l script.sh if the permissions shown are -rwxr--r--?Solution
Step 1: Break down the permission string
The string '-rwxr--r--' means: owner has read (r), write (w), execute (x); group has read (r) only; others have read (r) only.Step 2: Match permissions to options
Owner can read, write, execute; group can read only; others can read only correctly describes these permissions. Other options incorrectly assign write or execute permissions to group or others.Final Answer:
Owner can read, write, execute; group can read only; others can read only -> Option DQuick Check:
rwx r-- r-- = Owner rwx, group r, others r [OK]
- Misreading group or others permissions
- Confusing execute with write
- Ignoring the first dash meaning file type
chmod u+x file.txt but still cannot execute the file. What is the most likely reason?Solution
Step 1: Understand what chmod u+x does
This command adds execute permission for the file owner (user).Step 2: Consider why execution might still fail
If the file is not a script or executable program, adding execute permission won't let you run it.Final Answer:
The file is not a script or executable program -> Option CQuick Check:
Execute permission needs executable content [OK]
- Assuming ownership blocks execution
- Thinking others' execute permission is needed
- Confusing read permission with execute
Solution
Step 1: Understand chmod symbolic mode
"g=rw" sets group permissions to read and write only, removing execute if present.Step 2: Check other options
chmod g+rwx file.txt adds execute permission, which is not wanted. chmod o=rw file.txt changes others' permissions, and chmod u=rw file.txt changes user permissions.Final Answer:
chmod g=rw file.txt -> Option BQuick Check:
g=rw sets group read/write only [OK]
- Using g+rwx adds execute permission
- Changing user or others instead of group
- Using + instead of = to overwrite permissions
Ready to prove it?
Pass the test and this pattern moves unknown
