linux重定向多个文件,Linux命令之tee - 重定向输出到多个文件

用途说明

在执行Linux命令时,咱们能够把输出重定向到文件中,好比 ls >a.txt,这时咱们就不能看到输出了,若是咱们既想把输出保存到文件中,又想在屏幕上看到输出内容,就能够使用tee命令了。tee命令读取标 准输入,把这些内容同时输出到标准输出和(多个)文件中(read from standard input and write to standard output and files. Copy standard input to each FILE, and also to standard output. If a FILE is -, copy again to standard output.)。在info tee中说道:tee命令能够重定向标准输出到多个文件(`tee': Redirect output to multiple files. The `tee' command copies standard input to standard output and also to any files given as arguments.  This is useful when you want not only to send some data down a pipe, but also to save a copy.)。要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取。linux

经常使用参数

格式:teeweb

只输出到标准输出,由于没有指定文件嘛。less

格式: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.)this

格式:tee -a filespa

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

格式:tee -htm

输出到标准输出两次。(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.)blog

格式: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: *: 没有那个文件或目录

[root@web  ~]#

问题思考

相关资料

PS:2011.10.09 对此文件进行了编辑。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值