Redirect stderr and stdout to split files and one at a time in bash

I can redirect stdoutand stderrto split the files using:

dir >> out 2>> error

or stderrorand stdouttogether with one file, using:

dir >> consolidate 2>&1

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

+4
source share
2 answers

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
+5
source

There is no need for any bachisms, since this can easily be done in standard sh:

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

Source: https://habr.com/ru/post/1532244/


All Articles