nerdexam
CompTIA

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.

Shells, Scripting and Data Management

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

(22 responses)
  • A
    5% (1)
  • C
    91% (20)
  • D
    5% (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.

AThey are equivalent options with the same behaviour.

The options are not equivalent; -f is a stricter check than -e.

BThe -f option tests for a regular file. The -e option tests for an empty file.

The -e option tests for existence, not emptiness. The -s option checks for a non-empty file.

CBoth options check the existence of the path. The -f option also confirms that it is a regular file.Correct

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).

DThe -f option tests for a regular file. The -e option tests for an executable 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

#test command#file types#shell scripting

Community Discussion

No community discussion yet for this question.

Full LX0-104 Practice