tee命令用于读取规范输出的数据,将内容输入到屏幕,同时保留成文件,并且能够保留到多个文件。

如何应用tee

tee最根本的用法就是显示输入后果并且保留内容到文件中。上面例子应用free命令显示零碎内存应用信息,并应用tee命令将信息输入到屏幕,并保留到文件mem.txt中。

[root@localhost ~]# free -h | tee mem.txt

          total        used        free      shared  buff/cache   available

Mem: 1.8G 164M 1.2G 9.6M 387M 1.5G
Swap: 2.0G 0B 2.0G

能够查看一下mem.txt文件,能够看到输入内容曾经保留到mem.txt外面了。

写入到多个文件

tee能够写入多个文件,每个文件之间应用空格分隔。

[root@localhost ~]# free -h | tee mem1.txt mem2.txt mem3.txt

          total        used        free      shared  buff/cache   available

Mem: 1.8G 165M 1.2G 9.6M 389M 1.5G
Swap: 2.0G 0B 2.0G

在已存在的文件底部追加内容

上面的例子应用选项-a在文件底部追加内容,不笼罩原有内容。

[root@localhost ~]# free -h | tee -a mem.txt

          total        used        free      shared  buff/cache   available

Mem: 1.8G 165M 1.2G 9.6M 389M 1.5G
Swap: 2.0G 0B 2.0G

能够看到,在mem.txt文件底部追加了新的内容。

如果不想在屏幕输入内容,能够应用>规范输入符号,重定向到/dev/null中:

[root@localhost ~]# free -h | tee -a mem.txt > /dev/null

总结

tee命令用于读取规范输出的数据,将内容输入到屏幕,同时保留成文件,并且能够保留到多个文件。