user3431463
user3431463

Reputation: 59

Redirect stderr and stdout to separate files in Bash?

I am able to redirect stdout and stderr to separate files using:

dir >> out 2>> error

or stderror and stdout together to a single file using:

dir >> consolidate 2>&1

How can I do this together (get out, error, consolidate files at a time)?

Upvotes: 5

Views: 1018

Answers (2)

William Pursell
William Pursell

Reputation: 212584

There's no need for any bashisms, as this can easily be done in standard sh:

{ { dir | tee -a out; } 2>&1 >&3 | tee -a error; } >> consolidate 3>&1

Upvotes: 4

jaypal singh
jaypal singh

Reputation: 77175

You can try something like:

(command > >(tee out.txt) 2> >(tee error.txt >&2)) &> consol.txt

Test:

$ ls
f

$ ls g*
ls: cannot access g*: No such file or directory

$ (ls g f > >(tee out.txt) 2> >(tee error.txt >&2)) &> consol.txt

$ cat out.txt
f

$ cat error.txt
ls: cannot access g: No such file or directory

$ cat consol.txt
f
ls: cannot access g: No such file or directory

Upvotes: 6

Related Questions