學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

linux tee命令使用詳解教程

時(shí)間: 志藝942 分享

  在執(zhí)行Linux命令時(shí),我們既想把輸出保存到文件中,又想在屏幕上看到輸出內(nèi)容,就可以使用tee命令。接下來(lái)是小編為大家收集的linux tee 命令使用詳解教程,希望能幫到大家。

  linux tee 命令使用詳解教程

  tee

  功能說(shuō)明:讀取標(biāo)準(zhǔn)輸入的數(shù)據(jù),并將其內(nèi)容輸出成文件。

  語(yǔ)   法:tee [-ai][--help][--version][文件…]

  補(bǔ)充說(shuō)明:tee指令會(huì)從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),將其內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備,同時(shí)保存成文件。我們可利用tee把管道導(dǎo)入的數(shù)據(jù)存成文件,甚至一次保存數(shù)份文件。

  參   數(shù):-a 附加到既有文件的后面,而非覆蓋它。如果給予tee指令的文件名稱已經(jīng)存在,預(yù)設(shè)會(huì)覆蓋該文件的內(nèi)容。加上此參數(shù)后,數(shù)據(jù)會(huì)新增在該文件內(nèi)容的最后面,而不會(huì)刪除原先之內(nèi)容。

  -i 忽略中斷信號(hào)

  --help 在線幫助

  --version 顯示版本信息

  范   例:

  列出文本文件slayers.story的內(nèi)容,同時(shí)復(fù)制3份副本,文件名稱分別為ss-copy1、ss-copy2、ss-copy3:

  $ cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

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

  【功能】

  tee以標(biāo)準(zhǔn)輸入作為輸入,標(biāo)準(zhǔn)輸出和文件作為輸出。

  【舉例】

  tee file //覆蓋

  tee -a file //追加

  tee - //輸出到標(biāo)準(zhǔn)輸出兩次

  tee - - //輸出到標(biāo)準(zhǔn)輸出三次

  tee file1 file2 - //輸出到標(biāo)準(zhǔn)輸出兩次,并寫到那兩個(gè)文件中

  ls | tee file

  另:把標(biāo)準(zhǔn)錯(cuò)誤也被tee讀取

  ls "*" 2>&1 | tee ls.txt

  *用tee生成一個(gè)文件,包含你敲入的內(nèi)容:

  復(fù)制代碼

  代碼如下:

  $tee testfile

  這樣,會(huì)提示要你用標(biāo)準(zhǔn)輸入輸入內(nèi)容,然后敲回車會(huì)將你輸入的內(nèi)容寫入testfile和輸出到標(biāo)準(zhǔn)輸出,如果用[Ctrl]d結(jié)束輸入([Ctrl]c也行)。如果原來(lái)testfile有內(nèi)容,將會(huì)覆蓋。

  *把內(nèi)容追加到文件的末尾行:

  復(fù)制代碼

  代碼如下:

  $tee -a testfile

  結(jié)果類似上,不過(guò)如果原來(lái)testfile有內(nèi)容則不會(huì)覆蓋而是追加。

  *生成一個(gè)文件,敲入的時(shí)候,不接受中斷信號(hào):

  復(fù)制代碼

  代碼如下:

  $tee -i testfile

  結(jié)果同testfile,不過(guò)不會(huì)接收中斷信號(hào),只能用[Ctrl]d結(jié)束,而不能用[Ctrl]c了。

  *執(zhí)行l(wèi)s列出目錄文件同時(shí)將輸出保存到文件test中:

  復(fù)制代碼

  代碼如下:

  $ls | tee test

  這樣,會(huì)像平時(shí)一樣執(zhí)行l(wèi)s命令并將當(dāng)前目錄的文件名輸出到標(biāo)準(zhǔn)輸出。另外由于進(jìn)行了tee命令,所以會(huì)生成一個(gè)test文件,這個(gè)test文件的內(nèi)容和標(biāo)準(zhǔn)輸出的內(nèi)容一樣。

  【描述】

  tee指令會(huì)從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),將其內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備,同時(shí)保存成文件??梢杂糜诩认肟吹綐?biāo)準(zhǔn)輸出,又想將標(biāo)準(zhǔn)輸出保存到文件中的情況。

  參數(shù):

  -a或--append  附加到既有文件的后面,而非覆蓋它.

  -i-i或--ignore-interrupts  忽略中斷信號(hào)。

  --help  在線幫助。

  --version  顯示版本信息。

  常用參數(shù)

  格式:tee

  只輸出到標(biāo)準(zhǔn)輸出,因?yàn)闆](méi)有指定文件嘛。

  格式:tee file

  輸出到標(biāo)準(zhǔn)輸出的同時(shí),保存到文件file中。如果文件不存在,則創(chuàng)建;如果已經(jīng)存在,則覆蓋之。(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

  輸出到標(biāo)準(zhǔn)輸出的同時(shí),追加到文件file中。如果文件不存在,則創(chuàng)建;如果已經(jīng)存在,就在末尾追加內(nèi)容,而不是覆蓋。

  格式:tee -

  輸出到標(biāo)準(zhǔn)輸出兩次。(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 -

  輸出到標(biāo)準(zhǔn)輸出兩次,同時(shí)保存到file1和file2中。

  使用示例補(bǔ)充:

  示例一 tee命令與重定向的對(duì)比

  [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命令重復(fù)輸出字符串

  [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命令把標(biāo)準(zhǔn)錯(cuò)誤輸出也保存到文件

  [root@web ~]# ls "*"

  ls: *: 沒(méi)有那個(gè)文件或目錄

  [root@web ~]# ls "*" | tee -

  ls: *: 沒(méi)有那個(gè)文件或目錄

  [root@web ~]# ls "*" | tee ls.txt

  ls: *: 沒(méi)有那個(gè)文件或目錄

  [root@web ~]# cat ls.txt

  [root@web ~]# ls "*" 2>&1 | tee ls.txt

  ls: *: 沒(méi)有那個(gè)文件或目錄

  [root@web ~]# cat ls.txt

  ls: *: 沒(méi)有那個(gè)文件或目錄

  [root@web ~]#


看了“linux tee 命令使用詳解教程”還想看:

1.Linux系統(tǒng)中tee命令怎么用

2.Linux系統(tǒng)怎樣清理垃圾文件

3.CentOS等Linux系統(tǒng)清理系統(tǒng)垃圾和日志方法

linux tee命令使用詳解教程

在執(zhí)行Linux命令時(shí),我們既想把輸出保存到文件中,又想在屏幕上看到輸出內(nèi)容,就可以使用tee命令。接下來(lái)是小編為大家收集的linux tee 命令使用詳解教程,希望能幫到大家。 linux tee 命令使用詳解教程 tee 功能說(shuō)明:讀取標(biāo)準(zhǔn)輸入的數(shù)
推薦度:
點(diǎn)擊下載文檔文檔為doc格式
2835901