tee命令


用途說明

在執行Linux命令時,我們可以把輸出重定向到文件中,比如 ls >a.txt,

這時我們就不能看到輸出了,如果我們既想把輸出保存到文件中,又想在屏幕上看到輸出內容,就可以使用tee命令了。

tee命令讀取標准輸入,把這些內容同時輸出到標准輸出和(多個)文件中,tee命令可以重定向標准輸出到多個文件。要注意的是:在使用管道線時,前一個命令的標准錯誤輸出不會被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命令與重定向的對比

[root@web ~]# seq 5 >1.txt 
[root@web ~]# cat 1.txt 
1
2
3
4
5
[root@web ~]# cat 1.txt >2.txt 
[root@web ~]# cat 1.txt | tee 3.txt 
1
2
3
4
5
[root@web ~]# cat 2.txt 
1
2
3
4
5
[root@web ~]# cat 3.txt 
1
2
3
4
5
[root@web ~]# cat 1.txt >>2.txt 
[root@web ~]# cat 1.txt | tee -a 3.txt 
1
2
3
4
5
[root@web ~]# cat 2.txt 
1
2
3
4
5
1
2
3
4
5
[root@web ~]# cat 3.txt 
1
2
3
4
5
1
2
3
4
5
[root@web ~]#

 

示例二 使用tee命令重復輸出字符串

[root@web ~]# echo 12345 | tee 
12345

[root@web ~]# echo 12345 | tee - 
12345
12345
[root@web ~]# echo 12345 | tee - - 
12345
12345
12345
[root@web ~]# echo 12345 | tee - - - 
12345
12345
12345
12345
[root@web ~]# echo 12345 | tee - - - - 
12345
12345
12345
12345
12345
[root@web ~]#

[root@web ~]# echo -n 12345 | tee

12345[root@web ~]# echo -n 12345 | tee - 
1234512345[root@web ~]# echo -n 12345 | tee - - 
123451234512345[root@web ~]# echo -n 12345 | tee - - - 
12345123451234512345[root@web ~]# echo -n 12345 | tee - - - - 
1234512345123451234512345[root@web ~]#

 

示例三 使用tee命令把標准錯誤輸出也保存到文件

[root@web ~]# ls "*" 
ls: *: 沒有那個文件或目錄
[root@web ~]# ls "*" | tee - 
ls: *: 沒有那個文件或目錄
[root@web ~]# ls "*" | tee ls.txt 
ls: *: 沒有那個文件或目錄
[root@web ~]# cat ls.txt 
[root@web ~]# ls "*" 2>&1 | tee ls.txt 
ls: *: 沒有那個文件或目錄
[root@web ~]# cat ls.txt 
ls: *: 沒有那個文件或目錄
[root@web ~]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM