nerdexam
LPI

010-100 · Question #24

Which of the following commands redirects the output of cmd to the file foo.txt, in which an existing file is overwritten?

The correct answer is E. cmd > foo.txt. The > operator redirects a command's standard output to a file, overwriting the file if it already exists.

The Power of the Command Line

Question

Which of the following commands redirects the output of cmd to the file foo.txt, in which an existing file is overwritten?

Options

  • Acmd || foo.txt
  • Bcmd | foo.txt
  • Ccmd && foo.txt
  • Dcmd >> foo.txt
  • Ecmd > foo.txt

How the community answered

(16 responses)
  • A
    13% (2)
  • B
    6% (1)
  • C
    6% (1)
  • E
    75% (12)

Why each option

The > operator redirects a command's standard output to a file, overwriting the file if it already exists.

Acmd || foo.txt

|| is the logical OR operator that runs the right-hand command only if the left-hand command fails; it does not redirect output to a file.

Bcmd | foo.txt

| is the pipe operator that connects the standard output of cmd to the standard input of another command; foo.txt is not a command and this would fail.

Ccmd && foo.txt

&& is the logical AND operator that runs the right-hand command only if the left-hand command succeeds; it does not redirect output to a file.

Dcmd >> foo.txt

>> appends standard output to the file rather than overwriting it, so existing content is preserved.

Ecmd > foo.txtCorrect

The > operator in shell redirects standard output of cmd to foo.txt, truncating and overwriting the file if it exists, or creating it if it does not. This is the standard single-arrow output redirection in POSIX shells.

Concept tested: Shell output redirection to file with overwrite

Source: https://www.gnu.org/software/bash/manual/bash.html#Redirections

Topics

#output redirection#file overwrite#shell operators#stdout

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice