nerdexam
CompTIA

XK0-004 · Question #261

A Linux user needs to create a file named newfile in the home directory that mirrors the contents of the /etc/resolv.conf file. Which of the following commands would accomplish this task?

The correct answer is A. cat /etc/resolv.conf > /home/user/newfile. The cat command outputs a file's contents to standard output, and the > redirection operator writes that output to a new file, effectively mirroring the source file's contents.

System Management

Question

A Linux user needs to create a file named newfile in the home directory that mirrors the contents of the /etc/resolv.conf file. Which of the following commands would accomplish this task?

Options

  • Acat /etc/resolv.conf > /home/user/newfile
  • Becho /etc/resolv.conf > /home/user/newfile
  • Cgrep /etc/resolv.conf < /home/user/newfile
  • Dprintf /etc/resolv.conf > /home/user/newfile

How the community answered

(39 responses)
  • A
    92% (36)
  • B
    5% (2)
  • D
    3% (1)

Why each option

The cat command outputs a file's contents to standard output, and the > redirection operator writes that output to a new file, effectively mirroring the source file's contents.

Acat /etc/resolv.conf > /home/user/newfileCorrect

The cat command reads /etc/resolv.conf and sends its contents to standard output, while the > operator redirects that output to /home/user/newfile, creating the file if it does not exist. This combination accurately copies the full contents of the source file into the new destination file, satisfying the requirement to mirror the contents.

Becho /etc/resolv.conf > /home/user/newfile

echo /etc/resolv.conf writes the literal string '/etc/resolv.conf' as text into newfile rather than reading and outputting the file's actual contents.

Cgrep /etc/resolv.conf < /home/user/newfile

grep /etc/resolv.conf < /home/user/newfile uses newfile as input and searches it for the pattern '/etc/resolv.conf', which is reversed and does not copy the contents of resolv.conf.

Dprintf /etc/resolv.conf > /home/user/newfile

printf /etc/resolv.conf treats the file path as a format string and writes the literal string '/etc/resolv.conf' to newfile rather than reading and outputting the file's contents.

Concept tested: Using cat with output redirection to copy file contents

Source: https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html

Topics

#cat#shell redirection#file operations#resolv.conf

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice