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:

The 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.

gollark: Um...
gollark: (sorry, on mobile here)
gollark: Why define functions if you don't need them?
gollark: They're quite goodm
gollark: Maybe we should improve the general standard of it then.

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.