Linux tee命令用法

  • Linux tee命令用法已关闭评论
  • 71 views
  • A+
所属分类:系统知识

tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin。简单的说就是把数据重定向到给定文件和屏幕上。

[root@bogon ~]# tee --help
用法:tee [选项]... [文件]...
将标准输入复制到每个指定文件,并显示到标准输出。
 -a, --append 内容追加到给定的文件而非覆盖
 -i, --ignore-interrupts 忽略中断信号
 --help 显示此帮助信息并退出
 --version 显示版本信息并退出
如果文件指定为"-",则将输入内容复制到标准输出。

如何在 Linux 上使用这个命令?

我们以ping命令的结果为例吧,如下:

[root@bogon ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=51 time=66.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=51 time=65.6 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=51 time=58.7 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=51 time=62.5 ms
^C

然后同时,你想要输出的信息也同时能写入文件。这个时候,就可以通tee了,如下:

root@bogon ~]# ping www.baidu.com|tee output.txt
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=51 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=51 time=33.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=51 time=33.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=51 time=33.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=51 time=33.1 ms
^C[root@bogon ~]# more output.txt 
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=51 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=51 time=33.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=51 time=33.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=51 time=33.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=51 time=33.1 ms

如上,现在基本上了解了tee的基础命令了,如果需要给某个文件内容追加,则加上-a参数就行,命令如下:

ping www.baidu.com|tee -a output.txt

 tee也可以同时 写入多个文件

这个也很简单,文件名通过空格隔开,追加到后面就行:

[root@bogon ~]# ping www.baidu.com|tee output1.txt output2.txt output3.txt
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=54 time=35.9 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=54 time=36.1 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=3 ttl=54 time=35.6 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=4 ttl=54 time=35.6 ms
^C[root@bogon ~]# more output1.txt 
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=54 time=35.9 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=54 time=36.1 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=3 ttl=54 time=35.6 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=4 ttl=54 time=35.6 ms
[root@bogon ~]# more output2.txt 
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=54 time=35.9 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=54 time=36.1 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=3 ttl=54 time=35.6 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=4 ttl=54 time=35.6 ms
[root@bogon ~]# more output3.txt 
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=54 time=35.9 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=54 time=36.1 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=3 ttl=54 time=35.6 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=4 ttl=54 time=35.6 ms

tee命令联合使用

[root@bogon 123]# ls * | tee output.txt | wc -l
3
[root@bogon 123]# more output.txt 
123.txt
124.txt
324.txt

如上面的命令不仅会将文件名存入output.txt文件中,还会通过 wc命令让你知道输入到output.txt中的文件数目。

剩下的我就不多说了,看参数就行,更多信息,请查看 帮助文档.

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar