Unix: confusing use of the 'tee' command [duplicate]
Asked Answered
C

8

14

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?

Confiscable answered 18/4, 2009 at 23:23 Comment(2)
:w !sudo tee % https://mcmap.net/q/45113/-how-does-the-vim-quot-write-with-sudo-quot-trick-workLegislate
Here is a quickie on Tee grizzlybit.info/blog/how-to-use-tee-on-linuxViscus
W
15

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 
Wesla answered 26/8, 2016 at 11:49 Comment(1)
Maybe, you can then show how you redirect the output also to less when you already use sudo.Smothers
C
37

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.

Cruciate answered 18/4, 2009 at 23:28 Comment(1)
That's where the name comes from :-)Crushing
W
15

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 
Wesla answered 26/8, 2016 at 11:49 Comment(1)
Maybe, you can then show how you redirect the output also to less when you already use sudo.Smothers
H
12

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.

Higherup answered 18/4, 2009 at 23:28 Comment(1)
what if the stdout data is too large how does it push data to file ? by size or by time period ?Diminuendo
C
5

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
Confiscable answered 24/8, 2009 at 7:52 Comment(0)
M
2

Remember that the target of tee is not restricted to regular files, but can be to devices, FIFOs, etc. Also, you can pipe to another tee invocation, and so on. :-)

Madelainemadeleine answered 18/4, 2009 at 23:34 Comment(0)
M
1

I find that the tee command is very useful in debugging shell scripts that contain long pipelines. This is the tail-end of a ghastly shell script that is a decade overdue for a rewrite in Perl, but it still works. (It was last modified in 1998, as it happens.)

# If $DEBUG is yes, record the intermediate results.
if [ "$DEBUG" = yes ]
then
    cp $tmp.1 tmp.1
    cp $tmp.2 tmp.2
    cp $tmp.3 tmp.3
    tee4="| tee tmp.4"
    tee5="| tee tmp.5"
    tee6="| tee tmp.6"
    tee7="| tee tmp.7"
fi

# The evals are there in case $DEBUG was yes.
# The hieroglyphs on the shell line pass on any control arguments
# (like -x) to the sub-shell if they are set for the parent shell.
for file in $*
do
    eval sed -f $tmp.1 $file                $tee4 |
    eval sed -f $tmp.3                      $tee5 |
    eval sh ${-+"-$-"}                      $tee6 |
    eval sed -f $tmp.2                      $tee7 |
    sed  -e '1s/^[  ]*$/--@/' -e '/^--@/d'
done

The three sed scripts that are run are ghastly - I don't plan to show them. This is also a semi-decent use of eval. The normal temporary file names ($tmp.1, etc) are preserved by a fixed name (tmp.1, etc), and the intermediate results are preserved in tmp.4 .. tmp.7. If I were updating the command, it would use '"$@#"' instead of '$*' as shown. And, when I'm debugging it, then there is but one file in the argument list, so the trampling of the debug files is not an issue for me.

Note that if you need to do so, you can create several copies of the input at one time; there is no need to feed one tee command into another.

If anyone needs it, I have a variant of tee called tpipe which sends copies of the output to multiple pipelines instead of multiple files. It keeps going even if one of the pipelines (or standard output) terminates early. (See my profile for contact info.)

Mesozoic answered 19/4, 2009 at 0:58 Comment(0)
S
0

tee simply mirrors the output into a file that can be specified as the argument to tee.

In the case you show tee is called as the super user (via sudo) and its sole purpose is to write a file as the super user instead of the user that does the echo.

Scramble answered 18/4, 2009 at 23:28 Comment(0)
A
0

The tee command simply creates N+1 number of files, one copy passed to standard output and others to the arguments provided to tee (i.e., files) where N is the number of arguments passed to tee.

Altogether answered 31/10, 2014 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.