bashunixstdoutstderr

Redirecting stdout/stderr to multiple files


I was wondering how to redirect stderr to multiple outputs. I tried it with this script, but I couldn't get it to work quite right. The first file should have both stdout and stderr, and the 2nd should just have errors.

perl script.pl &> errorTestnormal.out &2> errorTest.out

Is there a better way to do this? Any help would be much appreciated. Thank you.


Solution

  • The command below will do what you want.

    perl script.pl 2>&1 >errorTestnormal.out | tee -a errorTestnormal.out > errorTest.out`
    

    This is a bit messy, so lets go through it step by step.

    So now, STDOUT gets printed to a file, and STDERR gets printed to STDOUT. We want put STDERR into 2 different files, which we can do with tee. tee appends the text it's given to a file, and also echoes to STDOUT.

    After this, errorTestnormal.out has all the STDOUT output, and then all the STDERR output. errotTest.out contains only the STDERR output.