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.
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)- A92% (36)
- B5% (2)
- D3% (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.
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.
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.
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.
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
Community Discussion
No community discussion yet for this question.