Bird
Raised Fist0
Bash Scriptingscripting~10 mins

File existence checks in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - File existence checks
Start
↓
Check if file exists
↓
End
The script checks if a file exists, then prints a message based on the result.
Execution Sample
Bash Scripting
filename="test.txt"
if [ -e "$filename" ]; then
  echo "File exists"
else
  echo "File does not exist"
fi
This script checks if 'test.txt' exists and prints a message accordingly.
Execution Table
StepActionConditionResultOutput
1Set filename to 'test.txt'N/Afilename='test.txt'
2Check if file exists with [ -e "$filename" ][ -e "test.txt" ]True if file exists, False if not
3If condition TrueFile exists?Yesecho 'File exists'
4Print outputN/AN/AFile exists
5If condition FalseFile exists?Noecho 'File does not exist'
6Print outputN/AN/AFile does not exist
💡 Script ends after printing the appropriate message based on file existence.
Variable Tracker
VariableStartAfter Step 1Final
filenameundefinedtest.txttest.txt
Key Moments - 3 Insights
Why do we use quotes around "$filename" in the condition?
Quoting "$filename" prevents errors if the filename contains spaces or is empty, ensuring the condition checks the correct string as shown in step 2 of the execution_table.
What does the -e option check in the condition?
The -e option checks if the file exists regardless of type (file, directory, etc.), as seen in step 2 where the condition evaluates file existence.
Why is there an else branch in the script?
The else branch handles the case when the file does not exist, ensuring the script always prints a clear message, demonstrated in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'filename' after step 1?
Aundefined
B"test.txt"
C"filename"
Dempty string
💡 Hint
Check the variable_tracker table under 'After Step 1' for 'filename'.
At which step does the script print 'File exists'?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Output' column in the execution_table for the message 'File exists'.
If the file does not exist, which step shows the printed output?
AStep 3
BStep 4
CStep 6
DStep 5
💡 Hint
Check the execution_table rows where the output is 'File does not exist'.
Concept Snapshot
File existence check in bash:
Use [ -e "$filename" ] to test if a file exists.
If true, run commands in then-block.
Else, run commands in else-block.
Always quote variables to handle spaces.
Print messages to confirm existence.
Full Transcript
This script sets a variable 'filename' to 'test.txt'. It then checks if this file exists using the condition [ -e "$filename" ]. If the file exists, it prints 'File exists'. Otherwise, it prints 'File does not exist'. Quoting the variable ensures the check works even if the filename has spaces or is empty. The script ends after printing the appropriate message.

Practice

(1/5)
1. Which command correctly checks if a file named data.txt exists in a bash script?
easy
A. if [ -e data.txt ]; then echo "File exists"; fi
B. if ( -e data.txt ); then echo "File exists"; fi
C. if [[ exists data.txt ]]; then echo "File exists"; fi
D. if test -f data.txt then echo "File exists"; fi

Solution

  1. Step 1: Understand the correct syntax for file existence check

    The test command with square brackets and -e checks if a file or directory exists.
  2. Step 2: Verify the command structure

    The command if [ -e data.txt ]; then ... fi is the correct syntax in bash for this check.
  3. Final Answer:

    if [ -e data.txt ]; then echo "File exists"; fi -> Option A
  4. Quick Check:

    Use [ -e filename ] to check existence [OK]
Hint: Use [ -e filename ] inside if for existence check [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using incorrect test operators like 'exists'
  • Missing semicolon or then keyword
2. Which of the following is the correct syntax to check if a file report.log exists and is a regular file in bash?
easy
A. if [ -d report.log ]; then echo "Exists"; fi
B. if [ -f report.log ]; then echo "Exists"; fi
C. if [ -e report.log ]; then echo "Exists"; fi
D. if test -x report.log; then echo "Exists"; fi

Solution

  1. Step 1: Identify the test for regular file

    The -f option checks if the file exists and is a regular file (not directory).
  2. Step 2: Confirm the syntax

    The syntax if [ -f report.log ]; then ... fi correctly checks for a regular file.
  3. Final Answer:

    if [ -f report.log ]; then echo "Exists"; fi -> Option B
  4. Quick Check:

    -f checks regular file existence [OK]
Hint: Use [ -f filename ] to check regular file existence [OK]
Common Mistakes:
  • Using -d which checks for directories
  • Using -x which checks for executable permission
  • Confusing -e (exists) with -f (regular file)
3. What will be the output of this script if myfile.txt does not exist?
if [ -e myfile.txt ]; then
  echo "Found"
else
  echo "Not Found"
fi
medium
A. Not Found
B. Syntax error
C. Found
D. No output

Solution

  1. Step 1: Understand the condition

    The condition [ -e myfile.txt ] checks if the file exists.
  2. Step 2: Evaluate the file existence

    Since myfile.txt does not exist, the condition is false, so the else branch runs.
  3. Final Answer:

    Not Found -> Option A
  4. Quick Check:

    File missing triggers else branch [OK]
Hint: If file missing, else branch runs [OK]
Common Mistakes:
  • Assuming output is 'Found' even if file missing
  • Confusing -e with other test operators
  • Missing else branch handling
4. Identify the error in this bash snippet that checks if config.cfg exists:
if [ -e config.cfg ]
  echo "Exists"
fi
medium
A. Echo command syntax is wrong
B. Wrong test operator -e used
C. Missing closing bracket ]
D. Missing then keyword after condition

Solution

  1. Step 1: Check if statement syntax

    Bash requires the then keyword after the condition in an if statement.
  2. Step 2: Identify missing part

    The snippet lacks then after [ -e config.cfg ], causing a syntax error.
  3. Final Answer:

    Missing then keyword after condition -> Option D
  4. Quick Check:

    if needs then after condition [OK]
Hint: Always put then after if condition [OK]
Common Mistakes:
  • Omitting then keyword
  • Forgetting semicolon before then on same line
  • Misplacing brackets
5. You want to write a bash script that checks if a file named 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?
hard
A. if [ -e backup.tar.gz ]; then touch backup.tar.gz; else echo "Backup found"; fi
B. if [ -f backup.tar.gz ]; then touch backup.tar.gz; else echo "Backup found"; fi
C. if [ -e "backup.tar.gz" ]; then echo "Backup found"; else touch backup.tar.gz; fi
D. if test -d backup.tar.gz; then echo "Backup found"; else touch backup.tar.gz; fi

Solution

  1. Step 1: Check file existence correctly

    Use [ -e "backup.tar.gz" ] to check if the file exists, quoting filename to handle spaces.
  2. Step 2: Execute correct commands based on condition

    If file exists, print "Backup found"; else create empty file with touch.
  3. Final Answer:

    if [ -e "backup.tar.gz" ]; then echo "Backup found"; else touch backup.tar.gz; fi -> Option C
  4. Quick Check:

    Check existence then else create file [OK]
Hint: Quote filenames and use if-else for existence and creation [OK]
Common Mistakes:
  • Swapping then and else commands
  • Using -d to check regular files
  • Not quoting filenames with spaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard