Tee is not useless
Maybe you knew that anyway? If not, read on! Or if you know how it works, but aren't sure why it exists, skip to the end to see how it fit in the with the Unix philosophy.
What is the purpose of
At its simplest, it takes data on standard input and writes that to standard output and one (or more) files. It has been likened to a plumbing tee piece in the way it splits one input into two outputs (and two directions).
Let's take your first example:
do_something | tee -a logfile
This takes the output of
do_something and appends it to logfile, while also displaying it to the user. In fact, the Wikipedia page on
tee has this as the second example:
To view and append the output from a command to an existing file:
lint program.c | tee -a program.lint
This displays the standard output of the lint program.c command at the computer and at the same time appends a copy of it to the end of the program.lint file. If the program.lint file does not exist, it is created.
The very next example has another use: escalation of permissions:
To allow escalation of permissions:
cat ~/.ssh/id_rsa.pub | ssh admin@server "sudo tee -a /root/.ssh/authorized_keys2 > /dev/null"
This example shows tee being used to bypass an inherent limitation in the
sudo is unable to pipe the standard output to a file. By dumping its standard out stream into
/dev/null, we also suppress the mirrored output in the console. The command above gives the current user root access to a server over ssh, by installing the user's public key to the server's key authorization list.
Or perhaps you want to take the output of one command, write that somewhere and also use that as input to another command?
You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.
The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substitution. After the substitution, it will be added as a new cron job.
$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –
(credit to Tee command usage examples)
Tee works with the Unix philosophy:
Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
(Credit to Basics of the Unix Philosophy)
tee fits all of these:
- it does one thing: creates an extra copy of input
- it works with other programs because it is the glue (or a 'T' plumbing piece if you prefer) that lets other programs work together as in the examples above
- it does this by manipulating a text stream given on standard input
teeand I/O redirection? The fact that it says “shell pipe redirections such as
>>” 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 of
teeinvented for those that don’t know you can do the same with shell pipe redirections?” and “What hidden features am I not seeing in
tee?”. At least two of those questions are too broad.