How to redirect stderr and stdout to different files and also display in terminal?
tee command as follows:
(cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log
3>&1 1>&2 2>&3 is how you swap stderr and stdout, because tee can only accept stdout.
Take a look at Unix tee command for more advanced redirections using
I think logging stdout and stderr to two different files is a splendid idea. Does it not make the logs asynchronous? So I tried out the following:
- stdout to "stdout.log" (as dogbane suggested)
- stderror to "stderr.log" (as dogbane suggested)
- all output to "all.log" and
- still be able to see the output on the display (in a separate terminal though!)
((cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log) &> all.log
in another terminal
tail -f --sleep-interval=2 all.log
I found another way too which saves both the streams approximately in the order as they would get printed without redirection.
command 2> >(tee errlog | tee -a bothLog > /dev/tty ) | tee outlog | tee -a bothLog
But this works only with the shells which supports process substitution.