shell命令之tee

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
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banche168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值