tee#

tee 是包含在 GNU Coreutils 內的標準輸入重新導向工具。

tee 的名稱源自於字母「T」,其功能就像一個 T 型接頭:它會從標準輸入(Standard Input)讀取資料,同時將內容輸出到「標準輸出(顯示器)」以及「一個或多個檔案」中。這在需要一邊查看指令執行結果,一邊將結果存檔紀錄(如編譯日誌、監控輸出)時非常有用。

Install#

$ sudo apt install coreutils

Setting in up#


Operate#

tee [options] [file…]

顯示結果並存入檔案 (會覆蓋舊檔)#

ls -l | tee output.txt

顯示結果並追加到檔案末尾#

echo “new log entry” | tee -a access.log

同時寫入多個檔案#

cat file.txt | tee backup1.txt backup2.txt

搭配 sudo 寫入權限受限的檔案 (常用技巧)#

echo “nameserver 8.8.8.8” | sudo tee /etc/resolv.conf

參數範例指令說明
-atee -a log.txt追加內容。不刪除原檔案內容,將新資料接在後面。
-itee -i data.txt忽略中斷。忽略 SIGINT 訊號。
-ptee -p output診斷錯誤。更精確地處理寫入非管道時的錯誤。
格式說明範例
| tee file顯示並儲存 (覆蓋)uptime | tee status.txt
| tee -a file顯示並儲存 (追加)date | tee -a history.log
| sudo tee file越權寫入檔案echo "data" | sudo tee /etc/config
| tee f1 f2同時寫入多個檔案echo "hello" | tee a.txt b.txt

Reference#

Official docs: