An administrator needs to append the output of a Linux command to an existing file for later analysis. Which of the following command-line strings should the administrator use?
The correct answer is D. ls >> file.txt because the >> (double greater-than) operator is specifically used in Linux to append output to an existing file without overwriting its current contents. This is a fundamental concept in shell redirection and is widely used in scripting and automation tasks.
In this command, ls generates a list of directory contents, and >> file.txt ensures that this output is added to the end of the file. If the file does not exist, it will be created automatically. If it does exist, the existing content remains intact, and the new output is appended below it.
Option A (cat ls > file.txt) is incorrect because cat ls attempts to read a file named ''ls,'' not execute the ls command. Additionally, > would overwrite the file instead of appending.
Option B (tee ls > awk file.txt) is invalid syntax and does not correctly use tee or redirection. The tee command is used to write output to both stdout and a file, but this example is malformed.
Option C (echo ls | sed -i file.txt) is incorrect because it does not execute the ls command; it simply echoes the string ''ls'' and attempts to modify a file using sed improperly.
From a Linux+ perspective, understanding redirection operators (>, >>, |) is essential for automation and scripting. The >> operator is especially important when logging outputs, collecting command results, or building reports over time without losing previous data.
Currently there are no comments in this discussion, be the first to comment!