LX0-104 · Question #118
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. The test -e path command checks if a file or directory exists at the specified path, whereas test -f path is more specific, checking if the path exists and refers to a regular file.
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
(22 responses)- A5% (1)
- C91% (20)
- D5% (1)
Why each option
The test -e path command checks if a file or directory exists at the specified path, whereas test -f path is more specific, checking if the path exists and refers to a regular file.
The options are not equivalent; -f is a stricter check than -e.
The -e option tests for existence, not emptiness. The -s option checks for a non-empty file.
test -e path evaluates to true if an entity (file, directory, symbolic link, socket, etc.) exists at the given path. In contrast, test -f path is a more restrictive check; it returns true only if the path exists and specifically refers to a regular file (e.g., not a directory or a device file).
The -e option tests for existence. The -x option tests for an executable file.
Concept tested: File existence and type checks in Bash (test command)
Source: https://linux.die.net/man/1/test
Topics
Community Discussion
No community discussion yet for this question.