Challenge - 5 Problems
File Existence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
The
-f test checks if a regular file exists.✗ Incorrect
The
[ -f filename ] condition returns true if the file exists and is a regular file. Since example.txt exists, it prints "File exists".💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
The
-d test checks for directories.✗ Incorrect
The
[ -d myfolder ] condition is false if the directory does not exist, so it prints "Directory missing".📝 Syntax
advanced2:00remaining
Identify the syntax error in file existence check
Which option contains a syntax error when checking if
data.log exists as a file?Attempts:
2 left
💡 Hint
Check for missing semicolons or line breaks before
then.✗ Incorrect
Option C is missing a semicolon or newline before
then, causing a syntax error.🚀 Application
advanced2: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
Attempts:
2 left
💡 Hint
The script prints only files that do NOT exist.
✗ Incorrect
Since
file1.txt exists, it is skipped. file2.txt does not exist, so it prints "file2.txt is missing".🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
-e checks if the file or link exists, -L checks if it is a link.✗ Incorrect
The
-e test returns true if the file or symbolic link exists, regardless of the target's existence. -f checks if the target is a regular file, -L checks if it is a symbolic link, and -d checks for directories.