Unix: confusing use of the Tee -command Unix: confusing use of the Tee -command unix unix

Unix: confusing use of the Tee -command


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.


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 


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.