linux tee的用法

tee命令用于将标准输入复制到每个指定文件,并显示到标准输出。tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

  • 基本参数
tee --help

Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.

  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
  -p                        diagnose errors writing to non pipes
      --output-error[=MODE]   set behavior on write error.  See MODE below
      --help     display this help and exit
      --version  output version information and exit

MODE determines behavior with write errors on the outputs:
  'warn'         diagnose errors writing to any output
  'warn-nopipe'  diagnose errors writing to any output not a pipe
  'exit'         exit on error writing to any output
  'exit-nopipe'  exit on error writing to any output not a pipe
The default MODE for the -p option is 'warn-nopipe'.
The default operation when --output-error is not specified, is to
exit immediately on error writing to a pipe, and diagnose errors
writing to non pipe outputs.
  • 将屏幕输出同时输出到文件:
ping www.baidu.com | tee pingbaidu.log
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
......
cat pingbaidu.com
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
......
  • 追加到输出文件
ping www.sina.com | tee -a pingbaidu.log
PING ww1.sinaimg.cn.w.alikunlun.com (47.246.16.231) 56(84) bytes of data.
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=1 ttl=57 time=2.15 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=2 ttl=57 time=5.42 ms
......
cat pingbaidu.log  
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=3 ttl=55 time=4.62 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=4 ttl=55 time=3.77 ms
PING ww1.sinaimg.cn.w.alikunlun.com (47.246.16.231) 56(84) bytes of data.
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=1 ttl=57 time=2.15 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=2 ttl=57 time=5.42 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=3 ttl=57 time=2.16 ms
......
  • 将内容输出到多个文件,我们直接在tee命令后面直接添加对应的文件
ping www.baidu.com | tee pingbaidu.log ping.log
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
......

cat pingbaidu.log 
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
......

cat ping.log 
ING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
  • 忽略中断事件,这个时候我们可以使用-i参数
$ ping www.baidu.com | tee -i pingbaidu.log ping.log
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=20.4 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.6 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.9 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=19.3 ms
^C
--- www.wshifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 19.311/19.842/20.465/0.450 ms

......

cat pingbaidu.log 
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=20.4 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.6 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.9 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=19.3 ms

--- www.wshifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 19.311/19.842/20.465/0.450 ms

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注