Linux tee命令

The tee command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy. Synopsis:

tee [option]… [file]…

If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously contained is overwritten unless the -a option is used.

tee命令把标准输入拷贝到标准输出和参数给出的多个文件,

在这个时候特别有用:不仅仅想把数据输入管道,也想保存一个拷贝。

The program accepts the following options. Also see Common options.

‘-a’

‘--append’

Append standard input to the given files rather than overwriting them.

‘-i’

‘--ignore-interrupts’

Ignore interrupt signals.

‘-p’    查了下centos某些版本好像不支持。

‘--output-error[=mode]’

Adjust the behavior with errors on the outputs, with the long form option supporting selection between the following modes:

该选项是用来改变在有错误的时候tee命令的表现行为,长选项支持以下模式:

‘warn’

Warn on error opening or writing any output, including pipes. Writing is continued to still open files/pipes. Exit status indicates failure if any output has an error.

‘warn-nopipe’

This is the default mode when not specified, or when the short form -p is used. Warn on error opening or writing any output, except pipes. Writing is continued to still open files/pipes. Exit status indicates failure if any non pipe output had an error.

‘exit’

Exit on error opening or writing any output, including pipes.

‘exit-nopipe’

Exit on error opening or writing any output, except pipes.

Note also that if any of the process substitutions (or piped stdout) might exit early without consuming all the data, the -p option is needed to allow tee to continue to process the input to any remaining outputs.

 

tee还有一个重要的应用场景是:

如果下载一个文件,想下载完成之后就立即计算出MD5值。

一般情况我们可以分两步执行,先下载完文件,再计算MD5.但是这种方式就需要两次操作下载的文件。

就可以使用tee来实现该功能:

 

curl http://host/url  | tee my.dvd | md5sum > result

tee把文件写到my.dvd同时计算出md5sum 重定向到result文件里面

有这样一种需求:

不但想计算md5还想计算sha1值,可以这样写

curl http://host/url  | tee >(md5sum > md5.result)  >(sha1sum >sha1.result)  

wget -O - https://example.com/dvd.iso \
  | tee >(sha1sum > dvd.sha1) \
        >(md5sum > dvd.md5) \
  > dvd.iso

其中>()这是进程替换Process Substitution,sh工作不了, zshbash, and ksh可以使用。

 

原文链接:

https://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html#tee-invocation

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值