tee
tee是一个很常用的命令,下面我们看看官方给出的使用指导文档
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
If a FILE is -, copy again to standard output.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'tee invocation'
-
tee的用途:用于将标准输入的结果拷贝到文件,并将结果输出拷贝到标准输出。
例如:ls -al | tee result.data -
使用方法及参数
-a, -append将结果追加到给定的一个或者多个文件中,不覆盖文件原有的内容
-i, --ignore-interrupts忽略掉中断信息
--help帮助文档
--version查看版本号 -
应用举例
下面重点介绍2个例子
1. cmd arg1 | tee file
我们切换到testshell目录下,执行 ls -al命令
[root@localhost testshell]# ls -al
total 4
drwxr-xr-x. 4 root root 26 Sep 12 11:40 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
然后运行如下命令
ls -al | tee text.txt
结果如下
[root@localhost testshell]# ls -al | tee text.txt
total 4
drwxr-xr-x. 4 root root 41 Sep 12 11:30 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
-rw-r--r--. 1 root root 0 Sep 12 11:30 text.txt
再观察text.txt中的内容,如下所示
[root@localhost testshell]# cat text.txt
total 4
drwxr-xr-x. 4 root root 41 Sep 12 11:30 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
-rw-r--r--. 1 root root 0 Sep 12 11:30 text.txt
这里有个细节就是这个命令里面的text.txt是什么时候被创建的,为啥在命令行和text.txt都出现了,大家可以思考一下是什么原理
tee的帮助文档是不是应该按照如下翻译更合适一些
用于将标准输入的结果拷贝到文件,然后再将结果输出拷贝到标准输出
2. 将结果追加到某个文件后面
准备
touch addcontent.txt && echo "old content" > addcontent.txt
执行
ls -al | tee -a text.txt
[root@localhost testshell]# ls -al | tee -a addcontent.txt
total 8
drwxr-xr-x. 4 root root 47 Sep 12 11:50 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
-rw-r--r--. 1 root root 12 Sep 12 11:51 addcontent.txt
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
再查看addcontent.txt中的内容如下:
[root@localhost testshell]# cat addcontent.txt
old content
total 8
drwxr-xr-x. 4 root root 47 Sep 12 11:50 .
dr-xr-x---. 16 root root 4096 Sep 12 11:30 ..
-rw-r--r--. 1 root root 12 Sep 12 11:51 addcontent.txt
drwxr-xr-x. 2 root root 6 Sep 12 11:30 awk
drwxr-xr-x. 2 root root 6 Sep 12 11:30 tee
327

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



