【Linux】tee命令

有时需将程序结果既在前台显示又写入文件,tee命令可实现此功能。其可用参数不多,可通过help命令查看。不加参数会覆盖文件内容,-a参数可追加,还能将输出写入多个文件,且输出可作为其他命令输入。

有的时候我们需要把程序结果既在前台显示,同时又写入文件中,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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值