nerdexam
LPI

102-500 · Question #178

What is the difference between the commands test -e path and test -f path?

The correct answer is C. Both options check the existence of the path. The -f option also confirms that it is a regular file. Option C is correct because -e checks whether anything exists at the given path (file, directory, symlink, device node, etc.), while -f goes further and also verifies the path refers specifically to a regular file - so every file that passes -f would also pass -e, but not the…

Shells and Shell Scripting

Question

What is the difference between the commands test -e path and test -f path?

Options

  • AThey are equivalent options with the same behaviour.
  • BThe -f option tests for a regular file. The -e option tests for an empty file.
  • CBoth options check the existence of the path. The -f option also confirms that it is a regular file.
  • DThe -f option tests for a regular file. The -e option tests for an executable file.

How the community answered

(39 responses)
  • A
    8% (3)
  • B
    3% (1)
  • C
    85% (33)
  • D
    5% (2)

Explanation

Option C is correct because -e checks whether anything exists at the given path (file, directory, symlink, device node, etc.), while -f goes further and also verifies the path refers specifically to a regular file - so every file that passes -f would also pass -e, but not the reverse.

  • A is wrong because the two options have distinct behaviors; -e is broader in scope than -f.
  • B is wrong because -e does not test for an empty file - that would be -s (which actually tests for a non-empty file). -e simply tests for existence.
  • D is wrong because -e has nothing to do with executability; the executable test is -x.

Memory tip: Think of -e as "exists" (anything at all) and -f as "file" (exists and is a regular file) - the letter matches the concept directly.

Topics

#test builtin#file existence#file type testing#shell conditionals

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice