4

How can I redirect stdout to 2 different files and stderr to another, different file?

I want something like <my_cmd> 1> file1 file2 2>file3

I know: <my_cmd> | tee file1 file2 > /dev/null

would redirect stdout to file1 and file2. But I also want stderr to be redirected to a different file. How can I achieve this?

  • 4
    Quick'n'dirty : <my_cmd> 1> file1 2>file3; cp file1 file2 – Httqm May 10 at 14:31
  • @Httqm, please, don't post answers as comments, but as answers which can be voted on. Your suggestion is not at all the same, in particular, there will be no relevant content in file2 until the command finishes! – ilkkachu May 10 at 21:54
11

This should do:

<my_cmd> 2>file3 | tee file1 file2 > /dev/null

The stderr redirection is done first and then tee gets just stdout.

  • 8
    can simplify a touch: cmd 2>file3 | tee file1 >file2 – glenn jackman May 10 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.