概述
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工作不了, zsh
, bash
, and ksh可以使用。
原文链接:
https://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html#tee-invocation
最后
以上就是幽默百合为你收集整理的Linux tee命令的全部内容,希望文章能够帮你解决Linux tee命令所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复