Output redirection: >
We want to redirect STDOUT
(file descriptor 1) to a file instead of a terminal, logging the output of each command instead of printing it out to the terminal in real-time.
# bash < commands.txt > output.log
Notice that there is no visible output in the terminal now – because the >
character has redirected output to output.log
. Use cat
to print out the logfile and confirm that it contains the expected output:
# cat output.log
/tmp/gopsinspect
hello there, friends
/bin/bash
/tmp
Use >>
to append output, without overwriting
In the previous example, we created a logfile by redirecting command output with >
. If you run the example a few times, you’ll notice that the logfile doesn’t grow at all. Each time you redirect output to a file with > filename
, anything in that file will be overwritten.
To avoid that – as in the case of a long-lived logfile that collects output from more than a single process or command &...