Linux tee命令詳解


一、tee命令介紹

  在執行Linux命令時,我們可以把輸出重定向到文件中,比如 ls >a.txt,這時我們就不能看到輸出了,如果我們既想把輸出保存到文件中,又想在屏幕上看到輸出內容,就可以使用tee命令。tee命令用於讀取標准輸入的數據,並將其內容輸出到文件中(文件不存在,則創建)。tee命令最基本的用法就是顯示輸出結果並且保存內容到文件中tee命令默認是覆蓋寫,可以使用-a選項來實現追加寫。tee命令的格式為:

[root@localhost ~]# tee [選項] 文件名

其中選項如下表所示:

-a 追加寫入文件,而非覆蓋寫入
-i 忽略中斷信號

 【例1】使用free命令顯示系統內存使用信息,並使用tee命令將信息保存到文件mem.txt中:

➜  test free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

➜  test free -h|tee mem.txt   # 把輸出內容保存到文件中(默認是覆蓋寫)      #要注意的是:在使用管道線時,前一個命令的標准錯誤輸出不會被tee讀取
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

➜  test cat mem.txt
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

tee mem.txt命令可以將輸出內容寫入到指定文件(mem.txt)中,tee也可以寫入多個文件,多個文件使用空格分隔,如:tee mem1.txt mem2.txt mem3.txt

【例2】使用tee命令將用戶的輸入保存到文件中

➜  test tee teefile.txt   # 用tee命令來把用戶輸入保存到指定文件中
My name is bcy           # 等待用戶輸入內容
My name is bcy           # 輸出數據,進行輸出反饋
hello bcy
hello bcy
^C

➜  test cat teefile.txt      
My name is bcy
hello bcy

二、在已經存在的文件底部追加內容

  上節介紹了tee命令可以將輸出寫入到文件中,默認是覆蓋寫入。我們可以使用-a選項來追加寫入

➜  test cat mem.txt
memory info as list:


➜  test free -h|tee -a mem.txt
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B


➜  test cat mem.txt
memory info as list:
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

可以看到,使用-a追加寫,並沒有覆蓋原有內容,推薦使用。

三、其他寫入文件的方式

  Linux中經常會用到將內容輸出到某文件當中,只需要在執行命令后面加上>或者>>號即可實現把內容寫入到文件中。

  >:將一條命令的執行結果(標准輸出,或者錯誤輸出,本來都要打印到屏幕上面的)重定向其它輸出設備,如文件文件不存在則創建,默認是覆蓋寫,>>是追加寫

  >>:把命令的執行結果追加寫入到文件中,文件不存在則創建

➜  test ll
total 28K
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  33 Sep 19 22:34 hello.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  25 Sep 20 17:46 teefile.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  25 Sep 13 10:53 temp.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  38 Sep 20 09:26 test.txt
➜ test ll
>a.txt # 覆蓋寫入a.txt,文件不存在,則創建 ➜ test cat a.txt # 成功寫入 total 28K -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 0 Sep 21 17:30 a.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 13 10:53 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 38 Sep 20 09:26 test.txt ➜ test echo "my name is bcy">>a.txt # 向a.txt中追加寫入 ➜ test cat a.txt # 寫入成功 total 28K -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 0 Sep 21 17:30 a.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 13 10:53 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 38 Sep 20 09:26 test.txt my name is bcy ➜ test echo "what's your name?">a.txt # 向a.txt中覆蓋寫入 ➜ test cat a.txt # 覆蓋原來的內容 what's your name?

 


免責聲明!

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



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