tee

用途说明

把linux命令输出重定向到文件中:
ls >a.txt,
但不能看到输出,如果我们既想把输出保存到文件中,
又想在屏幕上看到输出内容,就可以使用tee命令了。
tee命令读取标准输入,把这些内容同时输出到标准输出和(多个)文件中,
tee命令可以重定向标准输出到多个文件。
要注意的是:
   在使用管道线时,前一个命令的标准错误输出不会被tee读取。

常用参数

1、语法

1、只输出到标准输出,格式:tee
2、输出到标准输出的同时,保存到文件file中。
如果文件不存在,则创建;如果已经存在,则覆盖之。
格式:tee file
3、输出到标准输出的同时,追加到文件file中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。
格式:tee -a file
4、多次输出到标准设备,如
两次: tee - ,三次: tee -- 四次:tee ---
5、输出到标准设备多次,同时保存到file1/file2中。
格式:tee file1 file2 -
     tee file1 file2 --

2、示例

示例一 tee命令与重定向的对比 
[root@web ~]# seq 5 >1.txt 
[root@web ~]# cat 1.txt 
1
2
3
4
5
[root@web ~]# cat 1.txt >2.txt 
[root@web ~]# cat 1.txt | tee 3.txt 
1
2
3
4
5
[root@web ~]# cat 2.txt
1
2
3
4
5
[root@web ~]# cat 3.txt
1
2
3
4
5
[root@web ~]# cat 1.txt >>2.txt
[root@web ~]# cat 1.txt | tee -a 3.txt
1
2
3
4
5
[root@web ~]# cat 2.txt
1
2
3
4
5
1
2
3
4
5
[root@web ~]# cat 3.txt
1
2
3
4
5
1
2
3
4
5
[root@web ~]#
示例二 使用tee命令重复输出字符串
[root@web ~]# echo 12345 | tee
12345
[root@web ~]# echo 12345 | tee -
12345
12345
[root@web ~]# echo 12345 | tee - -
12345
12345
12345
[root@web ~]# echo 12345 | tee - - -
12345
12345
12345
12345
[root@web ~]# echo 12345 | tee - - - -
12345
12345
12345
12345
12345
[root@web ~]#
[root@web ~]# echo -n 12345 | tee
12345[root@web ~]# echo -n 12345 | tee -
1234512345[root@web ~]# echo -n 12345 | tee - -
123451234512345[root@web ~]# echo -n 12345 | tee - - -
12345123451234512345[root@web ~]# echo -n 12345 | tee - - - -
1234512345123451234512345[root@web ~]#
示例三 使用tee命令把标准错误输出也保存到文件
[root@web ~]# ls "*"
ls: *: 没有那个文件或目录
[root@web ~]# ls "*" | tee -
ls: *: 没有那个文件或目录
[root@web ~]# ls "*" | tee ls.txt
ls: *: 没有那个文件或目录
[root@web ~]# cat ls.txt
[root@web ~]# ls "*" 2>&1 | tee ls.txt
ls: *: 没有那个文件或目录
[root@web ~]# cat ls.txt
ls: *: 没有那个文件或目录
[root@web ~]#

《“tee”》 有 3 条评论

  1. Terrific post but I was wanting to know if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit
    more. Thanks!

回复 GRANIZO081 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注