常常会要使用到tee命令,但总是没有理解这个命令的一些细节,所以有时候使用总是不成功。最近再次学习到这个命令,有了更清晰的理解,今天整理小结一下。
1,命令含义
tee - read from standard input and write to standard output and files
说明,tee命令从stdin获取数据,然后一方面将数据发送给stdout,另一方面则发送给某文件;可以看出tee命令具有两重定向功能,将一个输入发送到两个输出中,如下示意图:
stdin ----(tee)---> stdout
----> file
2,命令使用
tee [option] file
注意点:a,tee只能从stdin获取,而不能从参数中获取,所以常常使用中为 cmd1 | tee ...;并且,stderr是会忽略的哦;
b,tee双重定向的一个方向为file,即某file名,当然也可以是'-',即stdin(其他/dev/stdin类似);
c,tee双重定向的另一个方向为stdout,不过很多时候可以将该stdout用管道重定向给其他命令,如 cmd1 | tee file | cmd2;
d,常用的option为-a,作用是以累加的形式将stdin加到file中去,类似于">>"之区别与">";
3,使用举例
1)基本使用
[root@localhost asd]# cat afile
aaaa
[root@localhost asd]# cat bfile
bbb
aaaa
bbb
[root@localhost asd]# cat cfile
aaaa
bbb
2)说明只能stdin获取,而stderr会被忽略
[root@localhost asd]# cat afile dfile | tee cfile
cat: dfile: 没有那个文件或目录
aaaa
[root@localhost asd]# cat cfile
aaaa
3)-a的使用
[root@localhost asd]# cat afile bfile | tee -a cfile
aaaa
bbb
[root@localhost asd]# cat cfile
aaaa
aaaa
bbb
4)tee重定向后stdout方向数据的使用
[root@localhost asd]# cat afile bfile | tee cfile | tr 'a-z' 'A-Z'
AAAA
BBB
[root@localhost asd]# cat cfile
aaaa
bbb
=====================================================(END)
*先小结这些我现在所知道的内容。
*有错误的地方,还请各位大侠帮忙指出,3Q。