首頁·終端機命令

tee

文字處理

用法

[cmd] | tee [file]

把輸入同時寫進檔案並送到螢幕;要寫 root 擁有的檔案就靠它,因為 sudo cmd > file 裡的重新導向是你自己的 shell 開啟的。

常用選項

選項含義
-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 裡的重新導向是你自己的 shell 開啟的。

Q. 怎麼寫?

[cmd] | tee [file] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 3 個,完整列表在 man tee。這條命令屬於文字處理。

相關命令

man 手冊: man tee