linux 命令之 tee
阿新 • • 發佈:2018-12-15
有時候,我們有這樣的需求:在螢幕上輸出資訊的同時又想寫入到檔案中,以便後續檢視。如果你想 "一步到胃",那麼可以採用tee 命令。
我們 man tee 可以看到:
tee - read from standard input and write to standard output and files (從標準輸入讀並寫入到標準輸出和檔案[自己翻譯的有點生硬] )。
SYNOPSIS tee [OPTION]... [FILE]... DESCRIPTION Copy standard input to each FILE, and also to standard output.
可以看到描述部分說,複製標準輸入到檔案,也可以到標準輸出。看到這部分有沒有想到什麼?反正我想到了,cat 命令。這句話的意思是說,它和 cat 的一些功能是類似的,好了廢話不多說,先體驗一把。
[email protected]:~/Code/Shell$ cat
w3
w3
xiaomu xiziz
xiaomu xiziz
woshi xiaomu xizi
woshi xiaomu xizi
^C
[email protected]:~/Code/Shell$ tee
zhang
zhang
xiaomu xizi
xiaomu xizi
w3
w3
^C
[email protected]:~/Code/Shell$
在這裡,建議在 linux 養成 man 的習慣,只有這樣,才會掌握第一手資料。
從標準讀入並寫到標準輸出和檔案:
[email protected]:~/Code/Shell$ tee self_report.txt
my blog name is called xiaomu xizi
my blog name is called xiaomu xizi
my blog address is :blog.csdn.net/zhang_referee
my blog address is :blog.csdn.net/zhang_referee
I love coding ,love writing
I love coding ,love writing
ok
ok
done
done
[email protected]:~/Code/Shell$ cat self_resport.txt
cat: self_resport.txt: 沒有那個檔案或目錄
[email protected]:~/Code/Shell$ cat self_report.txt
my blog name is called xiaomu xizi
my blog address is :blog.csdn.net/zhang_referee
I love coding ,love writing
ok
done
[email protected]:~/Code/Shell$
注意:man 文件中並沒有說,可串聯檔案和標準輸出(這是man cat 翻譯來的,原文:cat - concatenate files and print on the standard output ) ,所以不可像 cat 那樣從檔案讀入內容到標準輸出。
如果,你跟我一樣做了如下操作,那麼資料會丟失。
[email protected]:~/Code/Shell$ cat self_report.txt
my blog name is called xiaomu xizi
my blog address is :blog.csdn.net/zhang_referee
I love coding ,love writing
ok
done
[email protected]:~/Code/Shell$ echo "tee 命令示例:請勿隨意做以下操作,資料會丟失"
tee 命令示例:請勿隨意做以下操作,資料會丟失
[email protected]:~/Code/Shell$ echo "tee 命令示例:請勿隨意做以下操作,資料會丟失"
tee 命令示例:請勿隨意做以下操作,資料會丟失
[email protected]:~/Code/Shell$ tee self_report.txt
^C
[email protected]:~/Code/Shell$ cat self_report.txt
下面來看下,解決我們的需求問題 -- 輸出和寫入到檔案一步到位
[email protected]:~/Code/Shell$ ls -l | tee out.txt
總用量 44
-rw-rw-r-- 1 zhang zhang 32 10月 7 15:05 1.sh
-rw-rw-r-- 1 zhang zhang 76 10月 10 00:08 2.sh
-rw-rw-r-- 1 zhang zhang 344 10月 10 00:52 distinguish.sh
-rwxrw-r-- 1 zhang zhang 131 10月 9 00:33 exec_method.sh
-rw-rw-r-- 1 zhang zhang 52 10月 10 00:21 export.sh
prw-rw-r-- 1 zhang zhang 0 10月 7 17:40 fifo
-rw-rw-r-- 1 zhang zhang 187 10月 8 20:59 foreach.sh
-rw-rw-r-- 1 zhang zhang 199 10月 14 00:19 func.sh
-rw-rw-r-- 1 zhang zhang 225 10月 8 20:49 if_1.sh
-rw-rw-r-- 1 zhang zhang 280 10月 9 04:20 if.sh
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:12 out.txt
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:10 self_report.txt
-rw-rw-r-- 1 zhang zhang 161 10月 10 00:12 shift.sh
-rw-rw-r-- 1 zhang zhang 268 10月 8 21:26 while.sh
[email protected]:~/Code/Shell$ cat out.txt
總用量 44
-rw-rw-r-- 1 zhang zhang 32 10月 7 15:05 1.sh
-rw-rw-r-- 1 zhang zhang 76 10月 10 00:08 2.sh
-rw-rw-r-- 1 zhang zhang 344 10月 10 00:52 distinguish.sh
-rwxrw-r-- 1 zhang zhang 131 10月 9 00:33 exec_method.sh
-rw-rw-r-- 1 zhang zhang 52 10月 10 00:21 export.sh
prw-rw-r-- 1 zhang zhang 0 10月 7 17:40 fifo
-rw-rw-r-- 1 zhang zhang 187 10月 8 20:59 foreach.sh
-rw-rw-r-- 1 zhang zhang 199 10月 14 00:19 func.sh
-rw-rw-r-- 1 zhang zhang 225 10月 8 20:49 if_1.sh
-rw-rw-r-- 1 zhang zhang 280 10月 9 04:20 if.sh
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:12 out.txt
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:10 self_report.txt
-rw-rw-r-- 1 zhang zhang 161 10月 10 00:12 shift.sh
-rw-rw-r-- 1 zhang zhang 268 10月 8 21:26 while.sh
[email protected]:~/Code/Shell$
-a, --append
append to the given FILEs, do not overwrite
對同一檔案執行tee 命令 ,預設情況下,會覆蓋,使用 -a 引數,可追加
[email protected]:~/Code/Shell$ ls -l | grep out.txt | tee -a out.txt
-rw-rw-r-- 1 zhang zhang 759 10月 14 17:14 out.txt
[email protected]:~/Code/Shell$ cat out.txt
總用量 44
-rw-rw-r-- 1 zhang zhang 32 10月 7 15:05 1.sh
-rw-rw-r-- 1 zhang zhang 76 10月 10 00:08 2.sh
-rw-rw-r-- 1 zhang zhang 344 10月 10 00:52 distinguish.sh
-rwxrw-r-- 1 zhang zhang 131 10月 9 00:33 exec_method.sh
-rw-rw-r-- 1 zhang zhang 52 10月 10 00:21 export.sh
prw-rw-r-- 1 zhang zhang 0 10月 7 17:40 fifo
-rw-rw-r-- 1 zhang zhang 187 10月 8 20:59 foreach.sh
-rw-rw-r-- 1 zhang zhang 199 10月 14 00:19 func.sh
-rw-rw-r-- 1 zhang zhang 225 10月 8 20:49 if_1.sh
-rw-rw-r-- 1 zhang zhang 280 10月 9 04:20 if.sh
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:14 out.txt
-rw-rw-r-- 1 zhang zhang 0 10月 14 17:10 self_report.txt
-rw-rw-r-- 1 zhang zhang 161 10月 10 00:12 shift.sh
-rw-rw-r-- 1 zhang zhang 268 10月 8 21:26 while.sh
-rw-rw-r-- 1 zhang zhang 759 10月 14 17:14 out.txt
[email protected]:~/Code/Shell$
使用tee 命令寫入多個檔案
[email protected]:~$ ps -aux | grep gnome-system-monitor
zhang 14650 3.3 0.7 653532 60940 pts/11 Sl+ 17:27 0:00 gnome-system-monitor
zhang 14671 0.0 0.0 21312 940 pts/14 S+ 17:28 0:00 grep --color=auto gnome-system-monitor
[email protected]:~$ ps -aux | grep gnome-system-monitor | tee monitor.txt ubuntu_monitor.txt gnome_monitor.txt
zhang 14650 3.8 0.7 653532 60940 pts/11 Sl+ 17:27 0:02 gnome-system-monitor
zhang 14675 0.0 0.0 21312 968 pts/14 S+ 17:28 0:00 grep --color=auto gnome-system-monitor
[email protected]:~$ ls
Code Data examples.desktop gnome_monitor.txt monitor.txt ubuntu_monitor.txt 公共的 模板 視訊 圖片 文件 下載 音樂 桌面
[email protected]:~$ cat monitor.txt
zhang 14650 3.8 0.7 653532 60940 pts/11 Sl+ 17:27 0:02 gnome-system-monitor
zhang 14675 0.0 0.0 21312 968 pts/14 S+ 17:28 0:00 grep --color=auto gnome-system-monitor
[email protected]:~$ cat gnome_monitor.txt
zhang 14650 3.8 0.7 653532 60940 pts/11 Sl+ 17:27 0:02 gnome-system-monitor
zhang 14675 0.0 0.0 21312 968 pts/14 S+ 17:28 0:00 grep --color=auto gnome-system-monitor
[email protected]:~$ cat ubuntu_monitor.txt
zhang 14650 3.8 0.7 653532 60940 pts/11 Sl+ 17:27 0:02 gnome-system-monitor
zhang 14675 0.0 0.0 21312 968 pts/14 S+ 17:28 0:00 grep --color=auto gnome-system-monitor
[email protected]:~$
如何使用 tee 命令提升檔案寫入許可權?
假如你使用 Vim 編輯器 開啟檔案,並且做了很多更改,然後當你嘗試儲存修改時,你得到一個報錯,讓你意識到那是一個 root 所擁有的檔案,這意味著你需要使用 sudo 許可權儲存修改。
注意:請備份相關檔案,不要該出問題了。
esac
exit 0
username:zhang ,not root
# vim: noet ts=8
W12: 警告: 檔案 "networking" 已變動,並且在 Vim 中的緩衝區也已變動
進一步說明請見 ":help W12"
確定([O]), 載入檔案((L)):
上述操作需要輸入密碼。
更多資訊,請檢視幫助文件。