http://blog.163.com/shi_shun/blog/static/237078492011112695923852/
用途說明
在執行Linux指令時,我們可以把輸出重定向到檔案中,比如 ls >a.txt,這時我們就不能看到輸出了,如果我們既想把輸出儲存到檔案中,又想在螢幕上看到輸出内容,就可以使用tee指令了。tee指令讀取标準輸入,把這些内容同時輸出到标準輸出和(多個)檔案中(read from standard input and write to standard output and files. Copy standard input to each FILE, and also to standard output. If a FILE is -, copy again to standard output.)。在info tee中說道:tee指令可以重定向标準輸出到多個檔案(`tee': Redirect output to multiple files. The `tee' command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy.)。要注意的是:在使用管道線時,前一個指令的标準錯誤輸出不會被tee讀取。
常用參數
格式:tee
隻輸出到标準輸出,因為沒有指定檔案嘛。
格式:tee file
輸出到标準輸出的同時,儲存到檔案file中。如果檔案不存在,則建立;如果已經存在,則覆寫之。(If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously
contained is overwritten unless the `-a' option is used.)
格式:tee -a file
輸出到标準輸出的同時,追加到檔案file中。如果檔案不存在,則建立;如果已經存在,就在末尾追加内容,而不是覆寫。
格式:tee -
輸出到标準輸出兩次。(A FILE of `-' causes `tee' to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)
格式:tee file1 file2 -
輸出到标準輸出兩次,同時儲存到file1和file2中。
使用示例
示例一 tee指令與重定向的對比
[[email protected] ~]# seq 5 >1.txt
[[email protected] ~]# cat 1.txt
1
2
3
4
5
[[email protected] ~]# cat 1.txt >2.txt
[[email protected] ~]# cat 1.txt | tee 3.txt
1
2
3
4
5
[[email protected] ~]# cat 2.txt
1
2
3
4
5
[[email protected] ~]# cat 3.txt
1
2
3
4
5
[[email protected] ~]# cat 1.txt >>2.txt
[[email protected] ~]# cat 1.txt | tee -a 3.txt
1
2
3
4
5
[[email protected] ~]# cat 2.txt
1
2
3
4
5
1
2
3
4
5
[[email protected] ~]# cat 3.txt
1
2
3
4
5
1
2
3
4
5
[[email protected] ~]#
示例二 使用tee指令重複輸出字元串
[[email protected] ~]# echo 12345 | tee
12345
[[email protected] ~]# echo 12345 | tee -
12345
12345
[[email protected] ~]# echo 12345 | tee - -
12345
12345
12345
[[email protected] ~]# echo 12345 | tee - - -
12345
12345
12345
12345
[[email protected] ~]# echo 12345 | tee - - - -
12345
12345
12345
12345
12345
[[email protected] ~]#
[[email protected] ~]# echo -n 12345 | tee
12345[[email protected] ~]# echo -n 12345 | tee -
1234512345[[email protected] ~]# echo -n 12345 | tee - -
123451234512345[[email protected] ~]# echo -n 12345 | tee - - -
12345123451234512345[[email protected] ~]# echo -n 12345 | tee - - - -
1234512345123451234512345[[email protected] ~]#
示例三 使用tee指令把标準錯誤輸出也儲存到檔案
[[email protected] ~]# ls "*"
ls: *: 沒有那個檔案或目錄
[[email protected] ~]# ls "*" | tee -
ls: *: 沒有那個檔案或目錄
[[email protected] ~]# ls "*" | tee ls.txt
ls: *: 沒有那個檔案或目錄
[[email protected] ~]# cat ls.txt
[[email protected] ~]# ls "*" 2>&1 | tee ls.txt
ls: *: 沒有那個檔案或目錄
[[email protected] ~]# cat ls.txt
ls: *: 沒有那個檔案或目錄
[[email protected] ~]#