关于linux:Tee命令使用实例

25次阅读

共计 776 个字符,预计需要花费 2 分钟才能阅读完成。

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 命令用于读取规范输出的数据,将内容输入到屏幕,同时保留成文件,并且能够保留到多个文件。

正文完
 0