I'm trying to redirect error output to both a file and the terminal and throw away standard output, but I can't figure it out. Does anybody know how to do it?
2 Answers
myCommand 2>&1 1>/dev/null | tee /path/to/some/file.txt
STDOUT gets black-holed into /dev/null
STDERR gets redirected to STDOUT
tee receives STDOUT and re-echoes it as well as writing it to file
3 Comments
user219882
I was close :) I had this command and it didn't work
myCommand 1>/dev/null 2>&1 | tee /path/to/some/file.txt. Your solution works. Thank youJames C
I tried your solution first then remembered they should be the other way around ;)
Larry Morell
The order matters because the redirections are interpreted from left to right. Hence the first redirection sets sets stderr to the same stream as stdout. Then the second one switches stdout away.
See this post. You will need to use the tee command to direct in multiple directions.