前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tee命令

tee命令

作者头像
陪你听风
发布2021-03-31 23:38:42
1K0
发布2021-03-31 23:38:42
举报
文章被收录于专栏:陪你听风陪你听风

文章目录

用途说明

在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,

这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令了。

tee命令读取标准输入,把这些内容同时输出到标准输出和(多个)文件中,tee命令可以重定向标准输出到多个文件。要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取。

常用参数

格式:tee

只输出到标准输出,因为没有指定文件嘛。

格式:tee file

输出到标准输出的同时,保存到文件file中。如果文件不存在,则创建;如果已经存在,则覆盖之。(If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously contained is overwritten unless the `-a' option is used.)

格式:tee -a file

输出到标准输出的同时,追加到文件file中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。

格式:tee -

输出到标准输出两次。(A FILE of `-' causes `tee' to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)

格式:tee file1 file2 -

输出到标准输出两次,同时保存到file1和file2中。

使用示例

示例一 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: *: 没有那个文件或目录

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年1月6日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用途说明
  • 常用参数
  • 使用示例
    • 示例一 tee命令与重定向的对比
      • 示例二 使用tee命令重复输出字符串
        • 示例三 使用tee命令把标准错误输出也保存到文件
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档