This is more for personal reference than anything else. I wanted to log the output of different scripts i’d done but every time I wanted to I had to search the net bcause I kept forgetting (must be old age) so thought i’d put all the info in one place.
To redirect standard output to a file
echo "hello world" > test.txt
Display output as well as store into a file
Answer: tee
echo "hello world" | tee test.txt
To append the standard output to a file
echo"hello world" >> test.txt
Append to file and display it out as well
echo"hello world" | tee -a test.txt
standard output(stdout) – Normal print goes to this
standard error(stderr) – Error related messages go to this
Standard output is redirect to log.txt but stderr is print out.
./script > log.txt
What if I want stderr to be redirect and display the stdout?
./script 2> log.txt
I want both stored into the file.
./script 2&> log.txt
At last, I want both display and redirect to a file:
./script 2>&1 | tee log.txt (my favourite)