熱搜:win11繞過硬件限制安裝 一鍵重裝Win10系統(tǒng) 最干凈的u盤啟動盤 真正純凈版的win7系統(tǒng)
時間:2014-12-16 16:22:00 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時看資訊 2. 請使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20141216/32719.html 手機(jī)查看 評論 反饋
Linux下tee命令主要用于將管道導(dǎo)入的數(shù)據(jù)存成文件,那么tee命令有什么實際應(yīng)用呢?下面將通過實例來給大家詳細(xì)介紹下Linux tee命令的用法。
tee
功能說明:讀取標(biāo)準(zhǔn)輸入的數(shù)據(jù),并將其內(nèi)容輸出成文件。
語 法:tee [-ai][--help][--version][文件…]
補(bǔ)充說明:tee指令會從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),將其內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備,同時保存成文件。我們可利用tee把管道導(dǎo)入的數(shù)據(jù)存成文件,甚至一次保存數(shù)份文件。
參 數(shù):-a 附加到既有文件的后面,而非覆蓋它。如果給予tee指令的文件名稱已經(jīng)存在,預(yù)設(shè)會覆蓋該文件的內(nèi)容。加上此參數(shù)后,數(shù)據(jù)會新增在該文件內(nèi)容的最后面,而不會刪除原先之內(nèi)容。
-i 忽略中斷信號
--help 在線幫助
--version 顯示版本信息
范 例:
列出文本文件slayers.story的內(nèi)容,同時復(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)輸出兩次,并寫到那兩個文件中
ls | tee file
另:把標(biāo)準(zhǔn)錯誤也被tee讀取
ls “*” 2》&1 | tee ls.txt
*用tee生成一個文件,包含你敲入的內(nèi)容:
代碼如下:
$tee testfile
這樣,會提示要你用標(biāo)準(zhǔn)輸入輸入內(nèi)容,然后敲回車會將你輸入的內(nèi)容寫入testfile和輸出到標(biāo)準(zhǔn)輸出,如果用[Ctrl]d結(jié)束輸入([Ctrl]c也行)。如果原來testfile有內(nèi)容,將會覆蓋。
*把內(nèi)容追加到文件的末尾行:
代碼如下:
$tee -a testfile
結(jié)果類似上,不過如果原來testfile有內(nèi)容則不會覆蓋而是追加。
*生成一個文件,敲入的時候,不接受中斷信號:
代碼如下:
$tee -i testfile
結(jié)果同testfile,不過不會接收中斷信號,只能用[Ctrl]d結(jié)束,而不能用[Ctrl]c了。
*執(zhí)行l(wèi)s列出目錄文件同時將輸出保存到文件test中:
代碼如下:
$ls | tee test
這樣,會像平時一樣執(zhí)行l(wèi)s命令并將當(dāng)前目錄的文件名輸出到標(biāo)準(zhǔn)輸出。另外由于進(jìn)行了tee命令,所以會生成一個test文件,這個test文件的內(nèi)容和標(biāo)準(zhǔn)輸出的內(nèi)容一樣。
【描述】
tee指令會從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),將其內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備,同時保存成文件?梢杂糜诩认肟吹綐(biāo)準(zhǔn)輸出,又想將標(biāo)準(zhǔn)輸出保存到文件中的情況。
參數(shù):
-a或--append 附加到既有文件的后面,而非覆蓋它.
-i-i或--ignore-interrupts 忽略中斷信號。
--help 在線幫助。
--version 顯示版本信息。
常用參數(shù)
格式:tee
只輸出到標(biāo)準(zhǔn)輸出,因為沒有指定文件嘛。
格式:tee file
輸出到標(biāo)準(zhǔn)輸出的同時,保存到文件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)輸出的同時,追加到文件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)輸出兩次,同時保存到file1和file2中。
使用示例補(bǔ)充:
示例一 tee命令與重定向的對比
。踨oot@web ~]# seq 5 》1.txt
。踨oot@web ~]# cat 1.txt
1
2
3
4
5
。踨oot@web ~]# cat 1.txt 》2.txt
。踨oot@web ~]# cat 1.txt | tee 3.txt
1
2
3
4
5
。踨oot@web ~]# cat 2.txt
1
2
3
4
5
。踨oot@web ~]# cat 3.txt
1
2
3
4
5
。踨oot@web ~]# cat 1.txt 》》2.txt
。踨oot@web ~]# cat 1.txt | tee -a 3.txt
1
2
3
4
5
。踨oot@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
。踨oot@web ~]#
示例二 使用tee命令重復(fù)輸出字符串
。踨oot@web ~]# echo 12345 | tee
12345
。踨oot@web ~]# echo 12345 | tee -
12345
12345
。踨oot@web ~]# echo 12345 | tee - -
12345
12345
12345
。踨oot@web ~]# echo 12345 | tee - - -
12345
12345
12345
12345
[root@web ~]# echo 12345 | tee - - - -
12345
12345
12345
12345
12345
。踨oot@web ~]#
。踨oot@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)錯誤輸出也保存到文件
。踨oot@web ~]# ls “*”
ls: *: 沒有那個文件或目錄
。踨oot@web ~]# ls “*” | tee -
ls: *: 沒有那個文件或目錄
。踨oot@web ~]# ls “*” | tee ls.txt
ls: *: 沒有那個文件或目錄
。踨oot@web ~]# cat ls.txt
。踨oot@web ~]# ls “*” 2》&1 | tee ls.txt
ls: *: 沒有那個文件或目錄
。踨oot@web ~]# cat ls.txt
ls: *: 沒有那個文件或目錄
。踨oot@web ~]#
上面就是Linux下tee命令的用法介紹了,通過實例我們將更好的理解該命令的作用,并靈活用于實際當(dāng)中,你學(xué)會了嗎?
發(fā)表評論
共0條
評論就這些咯,讓大家也知道你的獨特見解
立即評論以上留言僅代表用戶個人觀點,不代表系統(tǒng)之家立場