tee

テキスト処理

書き方

[cmd] | tee [file]

受け取ったものをファイルにも画面にも同時に流します。root 所有のファイルに書くにはこれで、sudo cmd > file の > は自分のシェルが開くからです。

よく使うオプション

オプション意味
-aAppend to the file instead of overwriting it
-iIgnore Ctrl-C while the pipe runs
-pGNU: report errors when writing to a pipe

make 2>&1 | tee build.log

Watch the build and keep a copy.

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Appends to a root-owned file, which > cannot do.

curl -s api | tee raw.json | jq .name

Keeps the raw body and reads one field.

パイプでつないで使うのが本来の形です。ひとつで全部やるより、三つつないだ方が短くなります。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. tee は何をしますか。

受け取ったものをファイルにも画面にも同時に流します。root 所有のファイルに書くにはこれで、sudo cmd > file の > は自分のシェルが開くからです。

Q. どう打ちますか。

[cmd] | tee [file] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは3個です。全部は man tee にあります。このコマンドはテキスト処理の仲間です。

関連するコマンド

man ページ: man tee