0
0
Bash Scriptingscripting~20 mins

File existence checks in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Existence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Check if a file exists and print a message
What is the output of this script if the file example.txt exists in the current directory?
Bash Scripting
if [ -f example.txt ]; then
  echo "File exists"
else
  echo "File does not exist"
fi
AFile does not exist
BFile exists
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
The -f test checks if a regular file exists.
💻 Command Output
intermediate
2:00remaining
Check if a directory exists
What will this script output if the directory myfolder does NOT exist?
Bash Scripting
if [ -d myfolder ]; then
  echo "Directory found"
else
  echo "Directory missing"
fi
ASyntax error
BDirectory found
CNo output
DDirectory missing
Attempts:
2 left
💡 Hint
The -d test checks for directories.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in file existence check
Which option contains a syntax error when checking if data.log exists as a file?
Aif test -f data.log; then echo "Exists"; fi
Bif [[ -f data.log ]]; then echo "Exists"; fi
Cif [ -f data.log ] then echo "Exists"; fi
Dif [ -f data.log ]; then echo "Exists"; fi
Attempts:
2 left
💡 Hint
Check for missing semicolons or line breaks before then.
🚀 Application
advanced
2:00remaining
Script to check multiple files and report missing ones
Given the script below, what will be the output if file1.txt exists but file2.txt does not?
Bash Scripting
for file in file1.txt file2.txt; do
  if [ ! -f "$file" ]; then
    echo "$file is missing"
  fi
done
Afile2.txt is missing
B
file1.txt is missing
file2.txt is missing
Cfile1.txt is missing
DNo output
Attempts:
2 left
💡 Hint
The script prints only files that do NOT exist.
🧠 Conceptual
expert
2:00remaining
Understanding file existence test with symbolic links
Consider a symbolic link link.txt pointing to a regular file. Which test returns true if you want to check if link.txt exists regardless of target?
A[ -e link.txt ]
B[ -f link.txt ]
C[ -L link.txt ]
D[ -d link.txt ]
Attempts:
2 left
💡 Hint
-e checks if the file or link exists, -L checks if it is a link.