tee and process substitution: how to pipe one output to several processes
You can use tee command and something called ‘process substitution’ to pipe the output of a process to several processes at the same time.
tee
The tee
command copies standard input to standard output and also to any files given as arguments. A generic example:
<command> | tee <file> | <command>
As an example, if you want to save the output of this find
command for future processing but also do some filtering now with this output, you can type:
find . -type f -exec du -a {} + | tee du-files.txt | sort -nr | head -n1
You can add more than one file as an argument.
Append to a file
tee
overwrites a file unless -a
parameter is given.
Process substitution
Process substitution allows a process’ input or output to be referred to using a filename. It takes the form of >(COMMAND)
or <(COMMAND)
. We use the first form to provide input for COMMAND
from tee
. In this case COMMAND
needs to be a command that writes to a file, because the output of >(COMMAND)
can’t be piped.
Redirecting one output to several processes
You can add one or several process substitutions into tee
. The following example is a modified version of the find
example:
find . -type f -exec du -a {} + | tee >(gzip -9 > du-compressed.gz) | sort -nr | head -n1
- We save the output of
find
(which can be a big file) compressed and, at the same time, we do some filtering.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: