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…
Question
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)- A8% (3)
- B3% (1)
- C85% (33)
- D5% (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;
-eis broader in scope than-f. - B is wrong because
-edoes not test for an empty file - that would be-s(which actually tests for a non-empty file).-esimply tests for existence. - D is wrong because
-ehas 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
Community Discussion
No community discussion yet for this question.