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.
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)- A92% (48)
- B4% (2)
- C2% (1)
- D2% (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.
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.
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`.
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.
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
Community Discussion
No community discussion yet for this question.