nerdexam
CompTIA

XK0-005 · 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. To create a new file with the exact contents of an existing file, the cat command can be used to display the source file's content, and the output redirection operator > will save that content to the destination file.

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

(52 responses)
  • A
    92% (48)
  • B
    4% (2)
  • C
    2% (1)
  • D
    2% (1)

Why each option

To create a new file with the exact contents of an existing file, the `cat` command can be used to display the source file's content, and the output redirection operator `>` will save that content to the destination file.

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

The `cat` command displays the content of `/etc/resolv.conf` to standard output, and the `>` redirection operator then directs that output to create or overwrite `/home/user/newfile` with those exact contents.

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

The `echo` command would simply print the literal string "/etc/resolv.conf" into `/home/user/newfile`, not the content of the file `/etc/resolv.conf`.

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

The `grep` command searches for patterns, and in this case, it attempts to search for the literal string "/etc/resolv.conf" within the contents of `/home/user/newfile` (due to input redirection `<`), which is not the intended operation.

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

The `printf` command would output the literal string "/etc/resolv.conf" into `/home/user/newfile`, similar to `echo`, rather than the content of the specified file.

Concept tested: File content redirection

Source: https://man7.org/linux/man-pages/man1/cat.1.html

Topics

#File Management#Linux Commands#I/O Redirection#cat command

Community Discussion

No community discussion yet for this question.

Full XK0-005 Practice