0
0
Bash Scriptingscripting~10 mins

File test operators (-f, -d, -e, -r, -w, -x) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File test operators (-f, -d, -e, -r, -w, -x)
Start
Check file existence (-e)
Check if regular file (-f)
Output: It's a file
Check if directory (-d)
Output: It's a directory
Output: Exists but not file/dir
Output: Does not exist
Check permissions (-r, -w, -x)
Output permission results
End
The script checks if a file exists, then tests if it is a regular file or directory, and finally checks read, write, and execute permissions.
Execution Sample
Bash Scripting
#!/bin/bash
file="test.txt"

# Check existence
if [ -e "$file" ]; then
  echo "$file exists"
  
  # Check type
  if [ -f "$file" ]; then
    echo "$file is a regular file"
  elif [ -d "$file" ]; then
    echo "$file is a directory"
  else
    echo "$file exists but is neither a file nor directory"
  fi
  
  # Check permissions
  [ -r "$file" ] && echo "$file is readable" || echo "$file is not readable"
  [ -w "$file" ] && echo "$file is writable" || echo "$file is not writable"
  [ -x "$file" ] && echo "$file is executable" || echo "$file is not executable"
else
  echo "$file does not exist"
fi
Complete script that checks existence (-e), type (-f, -d), and permissions (-r, -w, -x) for 'test.txt', printing appropriate messages.
Execution Table
StepTestConditionResultAction/Output
1Check -e test.txtDoes test.txt exist?YesOutput: 'test.txt exists' & Proceed
2Check -f test.txtIs test.txt a regular file?YesOutput: 'test.txt is a regular file'
3Check -d test.txtIs test.txt a directory?No (skipped due to elif)No directory output
4Check -r test.txtIs test.txt readable?YesOutput: 'test.txt is readable'
5Check -w test.txtIs test.txt writable?NoOutput: 'test.txt is not writable'
6Check -x test.txtIs test.txt executable?NoOutput: 'test.txt is not executable'
7EndAll tests completed-Script ends
💡 Script handles all cases: existence, type, and permissions. Outputs vary based on actual file state.
Variable Tracker
Variable/TestStartAfter Step 1 (-e)After Step 2 (-f)After Step 3 (-d)After Step 4 (-r)After Step 5 (-w)Final (-x)
file"test.txt""test.txt""test.txt""test.txt""test.txt""test.txt""test.txt"
-e resultunsettruetruetruetruetruetrue
-f resultunsetunsettruetruetruetruetrue
-d resultunsetunsetunsetfalsefalsefalsefalse
-r resultunsetunsetunsetunsettruetruetrue
-w resultunsetunsetunsetunsetunsetfalsefalse
-x resultunsetunsetunsetunsetunsetunsetfalse
Key Moments - 4 Insights
Why check -e first before -f or -d?
The -f and -d tests require the file to exist; without -e, they may behave unexpectedly or cause issues if the path doesn't exist (see step 1).
What if the item exists but is neither -f nor -d (e.g., symlink, pipe)?
The updated script handles it with an else clause after -f/-d, outputting 'exists but neither file nor dir'.
How are permissions tested independently?
Each [-r], [-w], [-x] is tested separately with &&/|| for output, allowing mixed results as in steps 4-6.
Why use quotes around "$file"?
Prevents errors if file path has spaces or is unset/empty.
Visual Quiz - 4 Questions
Test your understanding
In the execution_table, what is the -d result at step 3?
AYes
BError
CNo (skipped due to elif)
DNot performed
💡 Hint
Step 3 'Result' column shows why no directory output.
Which step confirms it's a regular file?
A1
B2
C3
D4
💡 Hint
'Action/Output' in step 2 identifies the file type.
If -w was true, what would step 5 output?
ASkip to end
B'test.txt is not writable'
CNo change
D'test.txt is writable'
💡 Hint
Permission tests use conditional output based on result.
In variable_tracker, when does -x get evaluated?
AFinal column
BAfter step 2
CNever
DStep 1
💡 Hint
Track the last column for execute permission.
Concept Snapshot
Bash file test operators:
• -e: exists
• -f: regular file
• -d: directory
• -r: readable (by user)
• -w: writable
• -x: executable
Always quote paths: [ -e "$file" ]. Chain with if/elif for types; test perms independently.
Full Transcript
This VMC_v5 trace demonstrates bash file test operators in action. The script first verifies existence (-e) to safely proceed to type checks (-f, -d) and permissions (-r/-w/-x). Execution table simulates a 'test.txt' regular file that's readable but not writable/executable. Variable tracker shows test results evolving step-by-step. Key moments explain best practices like early existence check and handling special files. Updated execution_sample is complete with shebang and all tests. Quizzes reference tables for retention. Estimated 10 mins to review.