tee#
tee 是包含在 GNU Coreutils 內的標準輸入重新導向工具。
tee 的名稱源自於字母「T」,其功能就像一個 T 型接頭:它會從標準輸入(Standard Input)讀取資料,同時將內容輸出到「標準輸出(顯示器)」以及「一個或多個檔案」中。這在需要一邊查看指令執行結果,一邊將結果存檔紀錄(如編譯日誌、監控輸出)時非常有用。
Install#
$ sudo apt install coreutilsSetting 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
| 參數 | 範例指令 | 說明 |
|---|---|---|
-a | tee -a log.txt | 追加內容。不刪除原檔案內容,將新資料接在後面。 |
-i | tee -i data.txt | 忽略中斷。忽略 SIGINT 訊號。 |
-p | tee -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: