linux tee 命令详解




功能说明:读取标准输入的数据,并将其内容输出成文件。


语  法:tee [-ai][--help][--version][文件...]


补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。


参  数:
 -a或--append  附加到既有文件的后面,而非覆盖它.
 -i-i或--ignore-interrupts  忽略中断信号。
 --help  在线帮助。
 --version  显示版本信息。


[root@localhost ~]# who | tee who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
[root@localhost ~]# cat who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
[root@localhost ~]# pwd | tee -a who.out
/root
[root@localhost ~]# cat who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
/root
[root@localhost ~]#
本文出自 “起航工作室” 博客,请务必保留此出处http://liubin.blog.51cto.com/282313/131298


===================================================================

http://codingstandards.iteye.com/blog/833695


要注意的是:在使用管道线时,前一个命令的标准错误输出不会被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: *: 没有那个文件或目录
[root@web ~]#


更多相关文章

  1. 嵌入式Linux文件系统及其存储机制分析
  2. Oracle表按字段和|分格符导出文件
  3. linux 服务器间文件传输
  4. 拆分gzip压缩日志文件而不将未压缩的拆分存储在磁盘上
  5. Linux 环境变量与文件查找
  6. 为什么Linux不需要磁盘碎片整理?——借以复习文件系统方面的知识
  7. Linux中的文件权限和用户组
  8. Linux学习之文件系统权限及表示
  9. linux 文件系统的管理 (硬盘) 工作原理

随机推荐

  1. win10 编译 Android ffmpeg
  2. android cts 认证测试
  3. Android开机自启动设置
  4. Android上层启动过程的几个关键点
  5. 生成android自签名证书流程
  6. Android(安卓)Log 日志系统
  7. Android HttpURLConnection应用技巧分享
  8. Android属性动画之ObjectAnimator
  9. Android(安卓)左右布局
  10. ANT编译Android Eclipse工程