File existence checks in Bash Scripting - Time & Space Complexity
When checking if files exist in a script, it is important to understand how the time taken grows as the number of files increases.
We want to know how the script's running time changes when we check more files.
Analyze the time complexity of the following code snippet.
for file in "$@"; do
if [ -e "$file" ]; then
echo "$file exists"
else
echo "$file does not exist"
fi
done
This script loops through each file name given as input and checks if it exists, printing a message for each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that checks each file's existence one by one.
- How many times: Once for each file passed as input.
As the number of files increases, the script checks each file once, so the total work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 file existence checks |
| 100 | 100 file existence checks |
| 1000 | 1000 file existence checks |
Pattern observation: The number of checks grows directly with the number of files.
Time Complexity: O(n)
This means the time to run the script grows in a straight line as you add more files to check.
[X] Wrong: "Checking one file or many files takes the same time because each check is fast."
[OK] Correct: Even if one check is quick, doing many checks adds up, so more files mean more total time.
Understanding how loops over files affect time helps you write scripts that scale well and shows you can think about efficiency clearly.
"What if we checked files in nested directories recursively? How would the time complexity change?"
Practice
data.txt exists in a bash script?Solution
Step 1: Understand the correct syntax for file existence check
The test command with square brackets and-echecks if a file or directory exists.Step 2: Verify the command structure
The commandif [ -e data.txt ]; then ... fiis the correct syntax in bash for this check.Final Answer:
if [ -e data.txt ]; then echo "File exists"; fi -> Option AQuick Check:
Use [ -e filename ] to check existence [OK]
- Using parentheses instead of square brackets
- Using incorrect test operators like 'exists'
- Missing semicolon or then keyword
report.log exists and is a regular file in bash?Solution
Step 1: Identify the test for regular file
The-foption checks if the file exists and is a regular file (not directory).Step 2: Confirm the syntax
The syntaxif [ -f report.log ]; then ... ficorrectly checks for a regular file.Final Answer:
if [ -f report.log ]; then echo "Exists"; fi -> Option BQuick Check:
-f checks regular file existence [OK]
- Using -d which checks for directories
- Using -x which checks for executable permission
- Confusing -e (exists) with -f (regular file)
myfile.txt does not exist?if [ -e myfile.txt ]; then echo "Found" else echo "Not Found" fi
Solution
Step 1: Understand the condition
The condition[ -e myfile.txt ]checks if the file exists.Step 2: Evaluate the file existence
Sincemyfile.txtdoes not exist, the condition is false, so the else branch runs.Final Answer:
Not Found -> Option AQuick Check:
File missing triggers else branch [OK]
- Assuming output is 'Found' even if file missing
- Confusing -e with other test operators
- Missing else branch handling
config.cfg exists:if [ -e config.cfg ] echo "Exists" fi
Solution
Step 1: Check if statement syntax
Bash requires thethenkeyword after the condition in an if statement.Step 2: Identify missing part
The snippet lacksthenafter[ -e config.cfg ], causing a syntax error.Final Answer:
Missing then keyword after condition -> Option DQuick Check:
if needs then after condition [OK]
- Omitting then keyword
- Forgetting semicolon before then on same line
- Misplacing brackets
backup.tar.gz exists, and if it does, prints "Backup found", otherwise creates an empty file with that name. Which script snippet correctly does this?Solution
Step 1: Check file existence correctly
Use[ -e "backup.tar.gz" ]to check if the file exists, quoting filename to handle spaces.Step 2: Execute correct commands based on condition
If file exists, print "Backup found"; else create empty file withtouch.Final Answer:
if [ -e "backup.tar.gz" ]; then echo "Backup found"; else touch backup.tar.gz; fi -> Option CQuick Check:
Check existence then else create file [OK]
- Swapping then and else commands
- Using -d to check regular files
- Not quoting filenames with spaces
Ready to prove it?
Pass the test and this pattern moves unknown
