tee

提供: ArchWiki
2022年6月30日 (木) 13:52時点におけるKusanaginoturugi (トーク | 投稿記録)による版 (→‎Usage: 飜訳)
ナビゲーションに移動 検索に移動

Wikipedia より:

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

インストール

tee(1)coreutils パッケージの一部です。

使い方

ファイルにコンテンツを書き込むには

$ input stream | tee file1 file2...

デフォルトでは、tee は再度使用されると、ファイル内のコンテンツを上書きします。 ただし、必要に応じて、-a/--append 引数を使用してファイルにコンテンツを追加できます。

詳細は、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