LFCS · 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 option checks for the existence of any file or directory, while test -f specifically checks for the existence of 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
(37 responses)- A3% (1)
- B3% (1)
- C89% (33)
- D5% (2)
Why each option
The `test -e` option checks for the existence of any file or directory, while `test -f` specifically checks for the existence of a regular file.
The options `-e` and `-f` are distinct; `-e` checks for any existence, while `-f` checks for a regular file.
The `-e` option checks for general existence, not for an empty file; `test -s` is used to check for non-empty files.
The `test -e path` expression returns true if the specified `path` exists, regardless of whether it's a regular file, directory, symlink, or other file type. Conversely, `test -f path` returns true only if the `path` exists *and* refers specifically to a regular file (i.e., not a directory or other special file type).
The `-e` option checks for general existence, not for an executable file; `test -x` is used to check for executable permissions.
Concept tested: `test` command file type checks (`-e`, `-f`)
Source: https://man7.org/linux/man-pages/man1/test.1.html
Topics
Community Discussion
No community discussion yet for this question.