File test operators (-f, -d, -e, -r, -w, -x) in Bash Scripting - Time & Space Complexity
We want to see how the time to check file properties grows as we test many files.
How does running file tests on multiple files affect the total time?
Analyze the time complexity of the following code snippet.
for file in "$@"; do
if [ -f "$file" ]; then
echo "$file is a regular file"
elif [ -d "$file" ]; then
echo "$file is a directory"
elif [ -e "$file" ]; then
echo "$file exists but is not a regular file or directory"
else
echo "$file does not exist"
fi
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 each file passed as input for different properties like type and permissions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each file and running multiple file tests.
- How many times: Once per file in the input list.
Each file causes a fixed number of checks, so total work grows directly with number of files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the fixed checks |
| 100 | About 100 times the fixed checks |
| 1000 | About 1000 times the fixed checks |
Pattern observation: The total checks increase steadily as you add more files.
Time Complexity: O(n)
This means the time grows in a straight line with the number of files checked.
[X] Wrong: "Checking multiple file properties at once makes the script slower exponentially."
[OK] Correct: Each file test is a simple check, so doing several per file adds a fixed amount of work, not exponential growth.
Understanding how loops and repeated checks affect time helps you write scripts that scale well with input size.
"What if we nested the file tests inside another loop over file attributes? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of each operator
-fchecks if the path is a regular file,-dchecks for directory,-echecks if the path exists, and-xchecks if the file is executable.Step 2: Match the operator to the question
The question asks for the operator that checks if a path is a regular file, which is-f.Final Answer:
-f -> Option BQuick Check:
Regular file check = -f [OK]
- Confusing -f with -d
- Using -e which only checks existence
- Mixing -x (executable) with file type
Solution
Step 1: Identify the operator for directory check
-dis used to check if a path is a directory.Step 2: Verify the syntax correctness
The syntaxif [ -d /path/to/dir ]; then ... fiis correct for checking directory existence.Final Answer:
if [ -d /path/to/dir ]; then echo "Exists"; fi -> Option DQuick Check:
Directory check uses -d [OK]
- Using -f to check directories
- Confusing -e (exists) with directory check
- Missing spaces inside [ ] causing syntax errors
myfile.txt exists and is readable but not writable?if [ -r myfile.txt ]; then echo "Readable"; else echo "Not readable"; fi if [ -w myfile.txt ]; then echo "Writable"; else echo "Not writable"; fi
Solution
Step 1: Check readability condition
The file exists and is readable, so[ -r myfile.txt ]is true, printing "Readable".Step 2: Check writability condition
The file is not writable, so[ -w myfile.txt ]is false, printing "Not writable".Final Answer:
Readable\nNot writable -> Option CQuick Check:
Readable true, writable false = A [OK]
- Assuming writable if readable
- Mixing up -r and -w operators
- Ignoring else branch output
if [ -e myscript.sh -a -x ]; then echo "Executable file"; fi
Solution
Step 1: Analyze the -x operator usage
The-xoperator requires a filename argument, but none is provided after it.Step 2: Check other parts of the condition
-e myscript.shis valid to check existence; spaces around-aare correct; using single brackets is valid.Final Answer:
Missing filename after -x operator -> Option AQuick Check:
-x needs a file name [OK]
- Forgetting filename after -x
- Confusing -e and -f operators
- Missing spaces around operators causing syntax errors
config.cfg exists, is a regular file, and is readable and writable. Which condition correctly checks all these?Solution
Step 1: Identify the operator for regular file check
-fchecks if the path is a regular file, which is required.Step 2: Combine all conditions correctly
Using&&between separate[ ]tests is correct and clear. The script checks existence as a file, readability, and writability.Step 3: Eliminate incorrect options
if [ -e config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi uses-ewhich allows directories; if [ -e config.cfg -a -r config.cfg -a -w config.cfg ]; then echo "Ready"; fi uses-ewhich allows directories; if [ -d config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi checks for directory which is wrong.Final Answer:
if [ -f config.cfg ] && [ -r config.cfg ] && [ -w config.cfg ]; then echo "Ready"; fi -> Option AQuick Check:
Regular file with read/write uses -f, -r, -w combined [OK]
- Using -e instead of -f to check file type
- Using -a inside single [ ] causing confusion
- Checking directory instead of file
Ready to prove it?
Pass the test and this pattern moves unknown
