Linux tee命令
更多资料下载,冲击月薪过万,一线经验工程师录制,
干货满满的嵌入式视频教程,请点击淘宝店:wanglitao.taobao.com
Linux tee命令从标准输入设备读取数据,并写到标准输出设备和指定的文件上。
# tee input.txt
hello zhaixue.cc
hello zhaixue.cc
hello world!
hello world!
# ls
input.txt
# cat input.txt
hello zhaixue.cc
hello world!
使用tee命令后,shell就会进入输入交互状态,接收用户从标准输入设备(一般是键盘)输入的字符,将用户输入的字符显示到屏幕上,并写到指定的文件input.txt。
使用ctrl+D快捷键终止tee命令的运行,就会在当前目录下看到一个input.txt文件。文件的内容和用户的输入一致。当然,根据需要,你也可以输出到多个文件中:
# tee input1.txt input2.txt input3.txt
如果你在查看一个log,并想把这个log顺便保存到某个文件中,此时就可以考虑使用tee命令。比如你现在在使用ping命令ping一个网站:www.zhaixue.cc,并想把ping的结果保存到ping_log文件中,可以使用下面的命令:
# ping www.zhaixue.cc | tee ping_log
PING www.zhaixue.cc (121.199.61.48) 56(84) bytes of data.
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=1 ttl=128 time=44.3 ms
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=2 ttl=128 time=44.3 ms
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=3 ttl=128 time=44.2 ms
# ls
main.c ping_log
# cat ping_log
PING www.zhaixue.cc (121.199.61.48) 56(84) bytes of data.
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=1 ttl=128 time=44.3 ms
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=2 ttl=128 time=44.3 ms
64 bytes from 121.199.61.48 (121.199.61.48): icmp_seq=3 ttl=128 time=44.2 ms
使用tee命令,我们即看到了程序运行的输出结果,又顺便把打印信息保存到指定的文件ping_log中了,一举两得。
《Linux三剑客》视频教程,从零开始快速掌握Linux开发常用的工具:Git、Makefile、vim、autotools、debug,免费赠送C语言视频教程,C语言项目实战:学生成绩管理系统。详情请点击淘宝链接:Linux三剑客