Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix: confusing use of the Tee -command [duplicate]

Tags:

unix

tee

The manual states that tee is a "pipe fitting"-tool. The cases [1] confuse me:

1. case

echo "foo bar" | sudo tee -a /path/to/some/file

2. case

:w !sudo tee %

It is hard to understand the logic of tee from the cases. How does tee work?

like image 990
Léo Léopold Hertz 준영 Avatar asked Apr 18 '09 23:04

Léo Léopold Hertz 준영


4 Answers

tee is used to split a command pipeline, allowing you to save the output of a command to a file and send it along down the pipeline. In the first example you gave::

echo "foo bar" | sudo tee -a /path/to/some/file

"foo bar" will be echoed to standard output and appended to /path/to/some/file. Think of tee like a "T" joint in a pipe, splitting the output into two other pipes.

like image 72
Rick Copeland Avatar answered Nov 11 '22 23:11

Rick Copeland


tee is normally used to split the output of a program so that it can be both displayed and saved in a file. The command can be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output. It simultaneously copies the result into the specified file(s) or variables

tee [OPTION]... [FILE]...

For instance

tee [ -a ] [ -i ]... [ File ]...
  • -a Appends the output to the end of File instead of writing over it.

  • -i Ignores interrupts.

enter image description here

With sudo and appending to the file with your example in the question

ls -l | sudo tee -a file.txt 
like image 25
Girdhar Singh Rathore Avatar answered Nov 12 '22 01:11

Girdhar Singh Rathore


tee copies stdin to stdout (like cat) and additionally writes everything to the named file. Using it this way with sudo allows one to push information into a privileged mode and - at the same time - monitor whether the right stuff went there.

Also note, that due to the way redirection is handled in the shell the almost equivalent

sudo echo "foo bar" > /path/to/some/file

won't work, since the redirection would be done by the calling user and not by the sudo target user.

like image 34
David Schmitt Avatar answered Nov 11 '22 23:11

David Schmitt


Explanations for the Cases

1. the escalation of permissions with the sudo- and -tee commands

The example is not about just logic, rather convention. It shows the convention to escalate permissions:

echo "Body of file..." | sudo tee root_owned_file > /dev/null

This example shows tee being used to bypass an inherent limitation in the sudo command. sudo is unable to pipe the standard output to a file. By dumping its stdout stream into /dev/null, we also suppress the mirrored output in the console.

2. running sudo-commands with Vim

Since you can use Sudo-commands with Vim, you can use the command if you forgot to run as a sudo. It is useful in places such as /etc/init.d/, where you will find read-only files.

Logic with the tee-command

It is like a branch in Git, or better, please, see the T analogy by Rick Copeland. Hopefully, the modified example (original) helps to understand its use:

curl "http://en.wikipedia.org/wiki/Pipeline_(Unix)" | tee original_site | sed 's/[^a-zA-Z ]/ /g' | tr 'A-Z ' 'a-z\n' | grep '[a-z]' | sort -u | comm -23 - /usr/share/dict/words
like image 20
Léo Léopold Hertz 준영 Avatar answered Nov 11 '22 23:11

Léo Léopold Hertz 준영