Linux命令之tee - 重定向輸出到多個文件

用途說明

在執行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讀取。linux

 

經常使用參數

格式:teeweb

只輸出到標準輸出,由於沒有指定文件嘛。less

 

格式: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.)this

 

格式:tee -a filespa

輸出到標準輸出的同時,追加到文件file中。若是文件不存在,則建立;若是已經存在,就在末尾追加內容,而不是覆蓋。.net

 

格式:tee -htm

輸出到標準輸出兩次。(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.)blog

 

格式: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  ~]#

 

問題思考

相關資料

【1】Linux公社   linux tee命令詳解   
【2】百度知道    linux tee 命令的詳細使用, 越詳細越好.   
【3】腳本學習    linux tee命令: 將標準輸出一分爲二   
【4】起航工做室  linux tee 命令詳解

【5】5Linux教程   Linux tee command

 

PS:2011.10.09 對此文件進行了編輯。

相關文章
相關標籤/搜索