Interview › Scripting (Bash, Groovy)
What are the common file test operators (-f, -d, -e, -r, -w, -x)? [Basic]
Answer
Common file tests are -f for regular file, -d for directory, -e for exists, -r for readable, -w for writable, and -x for executable. They are used to validate preconditions before acting on files or directories.
Technical explanation
File tests avoid destructive errors such as overwriting a directory or trying to execute a non-executable file.
Combine tests with clear error messages so operational failures are easy to diagnose.
Remember permissions are evaluated from the current user's perspective, not just file mode bits.
Hands-on example
target="/var/log/app.log"[[ -e "$target" ]] || { echo "missing: $target" >&2; exit 1; }[[ -f "$target" && -r "$target" ]] || { echo "not a readable file" >&2; exit 1; }[[ -d /tmp ]] && echo "/tmp is a directory
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]