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

← All Scripting (Bash, Groovy) questions