歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux tee 命令使用詳解(大量實例)

linux tee 命令使用詳解(大量實例)

日期:2017/3/1 17:57:52   编辑:Linux技術

tee

  功能說明:讀取標准輸入的數據,並將其內容輸出成文件。
  語   法:tee [-ai][--help][--version][文件…]
  補充說明:tee指令會從標准輸入設備讀取數據,將其內容輸出到標准輸出設備,同時保存成文件。我們可利用tee把管道導入的數據存成文件,甚至一次保存數份文件。
  參   數:-a 附加到既有文件的後面,而非覆蓋它。如果給予tee指令的文件名稱已經存在,預設會覆蓋該文件的內容。加上此參數後,數據會新增在該文件內容的最後面,而不會刪除原先之內容。
       -i 忽略中斷信號
       --help 在線幫助
       --version 顯示版本信息

  范   例:

  列出文本文件slayers.story的內容,同時復制3份副本,文件名稱分別為ss-copy1、ss-copy2、ss-copy3:
  $ cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

tee [-ai][--help][--version][文件...]

【功能】

tee以標准輸入作為輸入,標准輸出和文件作為輸出。

【舉例】

tee file //覆蓋
tee -a file //追加

tee - //輸出到標准輸出兩次
tee - - //輸出到標准輸出三次

tee file1 file2 - //輸出到標准輸出兩次,並寫到那兩個文件中
ls | tee file

另:把標准錯誤也被tee讀取
ls "*" 2>&1 | tee ls.txt

*用tee生成一個文件,包含你敲入的內容:


復制代碼代碼如下:
$tee testfile

這樣,會提示要你用標准輸入輸入內容,然後敲回車會將你輸入的內容寫入testfile和輸出到標准輸出,如果用[Ctrl]d結束輸入([Ctrl]c也行)。如果原來testfile有內容,將會覆蓋。

*把內容追加到文件的末尾行:


復制代碼代碼如下:
$tee -a testfile

結果類似上,不過如果原來testfile有內容則不會覆蓋而是追加。


*生成一個文件,敲入的時候,不接受中斷信號:


復制代碼代碼如下:
$tee -i testfile

結果同testfile,不過不會接收中斷信號,只能用[Ctrl]d結束,而不能用[Ctrl]c了。

*執行ls列出目錄文件同時將輸出保存到文件test中:


復制代碼代碼如下:
$ls | tee test

這樣,會像平時一樣執行ls命令並將當前目錄的文件名輸出到標准輸出。另外由於進行了tee命令,所以會生成一個test文件,這個test文件的內容和標准輸出的內容一樣。

【描述】

tee指令會從標准輸入設備讀取數據,將其內容輸出到標准輸出設備,同時保存成文件。可以用於既想看到標准輸出,又想將標准輸出保存到文件中的情況。

參數:

 -a或--append  附加到既有文件的後面,而非覆蓋它.
 -i-i或--ignore-interrupts  忽略中斷信號。
 --help  在線幫助。
 --version  顯示版本信息。

常用參數
格式: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 ~]#

Copyright © Linux教程網 All Rights Reserved