tee
Wikipedia より:
- コンピュータでは、tee は標準ストリームを使用するコマンドラインインタプリタ(シェル)のコマンドで、標準入力を読み込んで標準出力と 1 つ以上のファイルの両方に書き込み、入力を効果的に複製するものです。主にパイプやフィルタと組み合わせて使用される。コマンド名は、配管工事で使用される T 字分岐にちなんで付けられています。
インストール
tee(1) は coreutils パッケージの一部です。
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.