Making scripts executable (chmod +x) in Bash Scripting - Time & Space Complexity
Let's explore how the time it takes to make a script executable changes as the number of scripts grows.
We want to know how the work increases when we use chmod +x on many files.
Analyze the time complexity of the following code snippet.
for file in *.sh; do
chmod +x "$file"
done
This script loops over all files ending with .sh and makes each one executable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
chmod +xcommand inside the loop. - How many times: Once for each
.shfile found.
As the number of script files grows, the total time grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times running chmod +x |
| 100 | 100 times running chmod +x |
| 1000 | 1000 times running chmod +x |
Pattern observation: The work grows directly with the number of files; double the files, double the work.
Time Complexity: O(n)
This means the time to make scripts executable grows linearly with the number of files.
[X] Wrong: "Running chmod +x on many files takes the same time as on one file."
[OK] Correct: Each file needs its own permission change, so more files mean more time.
Understanding how simple loops affect execution time helps you explain script performance clearly and confidently.
"What if we used a single chmod +x *.sh command instead of a loop? How would the time complexity change?"
Practice
chmod +x script.sh do in bash scripting?Solution
Step 1: Understand the
Thechmodcommandchmodcommand changes file permissions in Linux and Unix systems.Step 2: Interpret the
The+xoption+xoption adds execute permission, allowing the file to be run as a program.Final Answer:
Makes the scriptscript.shexecutable so it can be run directly. -> Option AQuick Check:
chmod +xmeans add execute permission [OK]
- Confusing execute permission with deleting files
- Thinking chmod copies files
- Mixing up ownership change with permission change
run.sh executable?Solution
Step 1: Recall the correct order of options in chmod
The correct syntax to add execute permission ischmod +x filename.Step 2: Check each option
chmod +x run.sh matches the correct syntax exactly; others have incorrect order or missing symbols.Final Answer:
chmod +x run.sh -> Option DQuick Check:
Correct syntax ischmod +x filename[OK]
chmod +x filename to add execute permission [OK]- Placing + and x in wrong order
- Using -x which removes execute permission
- Omitting the + sign
hello.sh without execute permission, what will be the output of the command sequence below?
ls -l hello.sh chmod +x hello.sh ls -l hello.sh
Solution
Step 1: Understand initial permissions
Initially,hello.shhas no execute permission, sols -lshows no 'x' in permissions.Step 2: Apply
After runningchmod +xand check againchmod +x hello.sh, execute permission is added, so the secondls -lshows 'x' in permissions.Final Answer:
The firstls -lshows no execute permission; the second shows execute permission added. -> Option BQuick Check:
Adding +x updates permissions visible inls -l[OK]
ls -l [OK]- Assuming execute permission is present initially
- Thinking chmod +x removes execute permission
- Not noticing permission changes in
ls -l
myscript.sh but get a "Permission denied" error. Which command fixes this issue?Solution
Step 1: Identify cause of "Permission denied"
The error usually means the script lacks execute permission.Step 2: Add execute permission
Usingchmod +x myscript.shadds execute rights, allowing the script to run.Final Answer:
chmod +x myscript.sh -> Option CQuick Check:
Add execute permission to fix "Permission denied" [OK]
chmod +x [OK]- Using -x which removes execute permission
- Setting permissions to 000 which denies all access
- Adding only read permission (+r) which doesn't allow execution
deploy.sh that you want to run directly by typing ./deploy.sh. However, it still says "Permission denied" after chmod +x deploy.sh. What could be the reason?Solution
Step 1: Confirm execute permission is set
Sincechmod +x deploy.shwas run, the script should have execute permission.Step 2: Consider filesystem limitations
Some mounted filesystems (like FAT32 or network shares) do not support Unix execute permissions, causing "Permission denied" despitechmod +x.Step 3: Evaluate other options
Adding read permission is usually default; renaming to .exe is irrelevant in Linux; running withbashworks but does not explain the permission error when running./deploy.sh.Final Answer:
The script is on a mounted filesystem that does not support execute permissions. -> Option AQuick Check:
Filesystem may block execute permission despite chmod [OK]
- Assuming chmod always works on all filesystems
- Thinking read permission alone allows execution
- Confusing script name extension with execution ability
Ready to prove it?
Pass the test and this pattern moves unknown
