有的时候我们需要把程序结果既在前台显示,同时又写入文件中,tee可以实现这种功能。
tee命令的可用参数并不多,可以通过help命令查看:
[oracle@master ~]$ 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
--help display this help and exit
--version output version information and exit
(1)不加参数,tee会把输出重定向到文件,文件中内容会被覆盖。
[oracle@master ~]$ echo 'This is first line.' > test.txt
[oracle@master ~]$ cat test.txt
This is first line.
[oracle@master ~]$ echo 'Hello Miss Dong.' | tee test.txt
Hello Miss Dong.
[oracle@master ~]$ cat test.txt
Hello Miss Dong.
(2)-a 参数把输出追加到文件。
[oracle@master ~]$ echo 'I love you more than I can say.' | tee -a test.txt
I love you more than I can say.
[oracle@master ~]$ cat test.txt
Hello Miss Dong.
I love you more than I can say.
(3) tee可以把输出写入多个文件,文件名之间用空格隔开。
[oracle@master ~]$ echo 'write the same output to more than one file.' | tee test1.txt test2.txt
write the same output to more than one file.
[oracle@master ~]$ cat test1.txt
write the same output to more than one file.
[oracle@master ~]$ cat test2.txt
write the same output to more than one file.
(4)tee命令的输出可以作为其他命令的输入。
比如下面的命令,不仅把输出写入test3.txt文件,还可以作为wc命令的输入,打印有多少文件名称写入test3.txt文件中。
[oracle@master test]$ ls *.txt | tee test3.txt | wc -l
3
[oracle@master test]$ cat test3.txt
test1.txt
test2.txt
test.txt
有时需将程序结果既在前台显示又写入文件,tee命令可实现此功能。其可用参数不多,可通过help命令查看。不加参数会覆盖文件内容,-a参数可追加,还能将输出写入多个文件,且输出可作为其他命令输入。
2999

被折叠的 条评论
为什么被折叠?



