Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Question
In linux, if you run the command "ls exiting_file.txt non-existing_file.txt", and the output on the prompt is:
ls: cannot access non-existing_file.txt: No such file or directory
-rw-rw-r-- 1 kevin kevin 6 Oct 17 16:03 existing_file.txt

Reissue the ls command above such that you capture all of the above output (i.e. the error message and the output line) to a file called "output.txt"?

Answer

ls existing_file.txt non-existing_file.txt > output.txt 2>&1
^^ note: the 2>&1, means redirect stderr (2) to stdout(1) and since things are executed from right to left this must be declared before the "> output.txt" part
^^ note: the first >, is same as 1> which means redirect stdout (i.e. 1) to output.txt

^^^ shorthand is just: ls existing_file.txt non-existing_file.txt &> output.txt


Question
In linux, if you run the command "ls exiting_file.txt non-existing_file.txt", and the output on the prompt is:
ls: cannot access non-existing_file.txt: No such file or directory
-rw-rw-r-- 1 kevin kevin 6 Oct 17 16:03 existing_file.txt

Reissue the ls command above such that you capture all of the above output (i.e. the error message and the output line) to a file called "output.txt"?

Answer
?

Question
In linux, if you run the command "ls exiting_file.txt non-existing_file.txt", and the output on the prompt is:
ls: cannot access non-existing_file.txt: No such file or directory
-rw-rw-r-- 1 kevin kevin 6 Oct 17 16:03 existing_file.txt

Reissue the ls command above such that you capture all of the above output (i.e. the error message and the output line) to a file called "output.txt"?

Answer

ls existing_file.txt non-existing_file.txt > output.txt 2>&1
^^ note: the 2>&1, means redirect stderr (2) to stdout(1) and since things are executed from right to left this must be declared before the "> output.txt" part
^^ note: the first >, is same as 1> which means redirect stdout (i.e. 1) to output.txt

^^^ shorthand is just: ls existing_file.txt non-existing_file.txt &> output.txt

If you want to change selection, open document below and click on "Move attachment"

8. Streams and sed -- The Stream Editor
ipe x into pipe y. Redirection is specified from right to left on the command-line. Hence, the above command means to mix stderr into stdout and then to redirect stdout to the file A . Finally, <span>ls existing_file non-existing_file 2>A 1>&2 cat A We notice that this has the same effect, except that here we are doing the reverse: redirecting stdout into stderr and then redirecting stderr into a file A . To see what happens

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.