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"Preparing for an interview?
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 difference between [ ], [[ ]], and (( )) in Bash? [Basic]
- How do you write a for loop over a list and a C-style for loop? [Basic]
- What do set -e, set -u, set -o pipefail, and set -x do? [Basic]
- What does an exit code of 0 versus non-zero mean? [Basic]
- What is $? used for, and how do you check the exit status of a command? [Basic]
- What does IFS do, and why set it when reading lines? [Basic]
- How do you write and call a function in Bash? [Basic]
- How do you pass arguments to a function and return a value or status? [Basic]