Think before you speak, read before you think.

tee命令

The tee utility copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.

将date执行的命令保存到文件里

 date > today.txt

将date的输出传到today.txt,且打印到屏幕上

 date | tee today.txt

结合管道命令,实现更多功能,比如cron文件如下

*/10 * * * * /usr/sbin/ntpdate 192.168.1.22
 15 5 * * * touch /tmp/nginx/fcgi

现在需要将cron备份,并将ip更换为192.168.1.88,使用tee可实现备份并修改

crontab -l | tee cron.backup | sed 's/192.168.1.22/192.168.1.88/g' | crontab -

tee后面跟文件,会默认将输出覆盖到文件中,如果使用追加模式,加-a

date | tee -a today.txt

如果追加到多个文件

date | tee today1.txt today2.txt today3.txt

在vim中当用户没有权限保存文件时,使用root来tee输出到此文件(%在vim中表示当前文件),这时会提示文件有变动,就会保存成功。

:w !sudo tee % > /dev/null


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *