So 1 represents stdout and 2 represents stderr. Based upon the information we can:

  • Redirect stdout/stderr to a file
  • Redirect stdout to a stderr OR redirect stderr to a stdout
  • To redirect stderr and stdout to a file
  • We can redirect stderr and stdout to stdout too
  • And finally you can redirect stderr and stdout to stderr

How to save terminal output to a file

By default, the command sends outputs to stdout and can be redirected to the file using the following syntax:

command > filename.txt

For example, save the date command output to a file named mydate.txt, run:

date > mydate.txt

To view file contains use the cat command:

cat mydate.txt

Feed data to our commands (input redirection)

We can read input from a file using the following simple syntax and the file must already exist:

command < input.txt

cat < /etc/passwd

wc -l < /etc/passwd

#linux #unix bash #nixcraft

Save terminal output to a file under Linux or Unix bash - nixCraft
1.55 GEEK