tee
is far from useless. I use it all the time and am glad it exists. It's a very useful tool if you have a pipeline that you want to split up. A very simple example is that you have some directory $d
that you want to tar and you also want to hash it because you're paranoid (like I am) and don't trust the storage medium to hold the data reliably. You could write it to the disk first and then hash it, but that'd fail if the archive gets corrupted before it's hashed. Furthermore, you'd have to read it and if you work on files that are several hundred GB in size a lot, you will know that you really don't want to read them again if it doesn't have to be.
So what I do is simply this:
tar -c "$d" | tee >(sha256sum) >(cat > "$d"".tar") > /dev/null
It creates the tar ball and pipes it to tee which then pipes it to two sub-shells, in one of which it's hashed and in the other of which it's written to the disk.
It's also great if you want to perform several operations on a big file:
< file.tar.gz tee >(sha256sum) >(tar -xz) /other/storage/location/file.tar.gz > /dev/null
Reads the file once, hashes it (so you can check whether it's still as it should be), extracts it, and copies it to a different location. No need to read it three times for that.
62How was this not answered by the very first line of the man page "...and write to standard output and files"? The answers are interesting, but broadly talking about how pipes are useful just reinforces how this Q seems too broad, and should perhaps have been closed. – Xen2050 – 2018-09-12T01:17:07.620
3
@Xen2050 A question cannot be blamed for a too-broad answer. The question is very specific, as is the current highest rated answer.
– Jon Bentley – 2018-09-14T09:34:16.8301
@JonBentley the question doesn't sound like "a specific problem with enough detail to identify an adequate answer" (as the close dialog reads). It does sound like this: "if your question could be answered by an entire book, or has many valid answers (but no way to determine which - if any - are correct), then it is probably too broad for our format." (Source: the Help Center)
– Xen2050 – 2018-09-15T08:02:12.9134@Xen2050 Are we reading the same question? It seems highly specific to me - what is the difference between tee and pipes? It has been adequately answered using two sentences. Far from an entire book. The fact that some answers choose to go off on a tangent has nothing to do with the scope of the question. – Jon Bentley – 2018-09-15T10:43:02.407
@JonBentley: Are we reading the same question? R Moog kind-of sort-of implies a fairly well focused question — what is the difference between
tee
*and I/O redirection?* The fact that it says “shell *pipe redirections* such as>
and>>
” is not a point in its favor, and is an argument for closing as unclear. But it actually asks multiple questions: “What is the purpose oftee
?”, “Istee
invented for those that don’t know you can do the same with shell pipe redirections?” and “What hidden features am I not seeing intee
?”. At least two of those questions are too broad. – G-Man Says 'Reinstate Monica' – 2018-09-21T02:32:04.707@G-Man You have to go out of your way to make that interpretation. It is obvious that the fundamental – Jon Bentley – 2018-09-21T05:05:04.883
I “have to go out of [my] way to make that interpretation”? Huh? I’m *quoting!* – G-Man Says 'Reinstate Monica' – 2018-09-21T05:07:46.030