tee

From ArchWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Merge-arrows-2.pngThis article or section is a candidate for merging with sudo.Merge-arrows-2.png

Notes: Some content is more about "how to use sudo" in some corner cases, than "how to use tee". (Discuss in Talk:Tee)

From Wikipedia:

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

Installation

tee(1) is part of the coreutils package.

Usage

To write content to files:

$ input stream | tee file1 file2...

By default, tee overwrites content in files when used again. However, if you want, you can append content in files by using the -a/--append argument.

For more usage, see tee(1).

Tips and tricks

Write to protected files

tee is very useful for writing to protected files:

$ input stream | sudo tee --option protected file1 protected file2...

when a simple >/>> would not have worked because of permissions.

Vim

Similar concept is useful when you forgot to start Vim with sudo when editing a configuration file owned by root. In this case you can do the following inside Vim to save the file:

:w !sudo tee %

You can add this to your ~/.vimrc to make this trick easy-to-use with :w!! mapping in command mode:

~/.vimrc
" Allow saving of files as sudo when I forgot to start vim using sudo
 cmap w!! w !sudo tee > /dev/null %

The > /dev/null part explicitly throws away the standard output since we do not need to pass anything to another piped command.

More detailed explanation of how and why this works can be found in How does the vim “write with sudo” trick work? article on StackOverflow.

See also