用途说明
   在执行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读取。
   
   常用参数
  
格式:
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: *:
没有那个文件或目录
   示例四 列出文本内容,同时复制3份复本
  
列出文本文件slayers.story的内容,同时复制3份副本,文件名称分别为ss-copy1、ss-copy2、ss-copy3:
  
[root@web ~]#  cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

Linux Shell脚本入门:tee命令的更多相关文章

  1. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  2. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  3. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  4. Linux shell 脚本入门教程+实例

    原文:http://www.wiquan.com/article/136 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是shell仍然是一个非常灵活的工具.She ...

  5. Linux Shell脚本入门--(linux空设备文件和重定向)>/dev/null 2>&1

    linux空设备文件和重定向 输出/输入重导向 >      >>   <   <<   :>   &>   2&>   2< ...

  6. Linux Shell脚本入门--awk命令详解

    简单使用: awk :对于文件中一行行的独处来执行操作 . awk -F :'{print $1,$4}'   :使用‘:’来分割这一行,把这一行的第一第四个域打印出来 . 详细介绍: AWK命令介绍 ...

  7. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  8. Linux Shell脚本入门--Uniq命令

    uniq uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [ ...

  9. Linux Shell脚本入门--wc命令

    wc 统计文件里面有多少单词,多少行,多少字符. wc语法 [root@www ~]# wc [-lwm] 选项与参数: -l :仅列出行: -w :仅列出多少字(英文单字): -m :多少字符: 默 ...

随机推荐

  1. Oracle 动态视图5 V$SESSION_LONGOPS

    一.视图V$SESSION_LONGOPS显示运行超过6秒的操作的状态.包括备份,恢复,统计信息收集,查询等等 Column Datatype Description SID NUMBER Sessi ...

  2. ios7.0结合storyborad实现页面跳转的总结

    折腾了一整天,本文总结一下ios7.0页面跳转有关的内容 storyboard的潜规则 我接触ios很晚,环境已经是xcode5+ios7,所以对以前的IOS开发模式并不了解.在网上查阅了很多资料,发 ...

  3. Yii2 composer更新后报错: Calling unknown method: yii\web\UrlManager::addRules()

    网上搜了好多答案,基本上都是让你在composer.json里面添加这样一段话: "extra": { "asset-installer-paths": { & ...

  4. Javascript实例:求数组中最大、最小值及下标

    题目:定义一个数组,并给出7个整数,求该数组中的最大值,及最大值下标,最小值及最小值下标.<script type="text/javascript">//定义一个数组 ...

  5. js SVG

    Snap.svg Paths.js http://www.sitepoint.com/creating-animated-valentines-day-card-snap-svg/

  6. 7zip 命令行

    转自 http://www.cnblogs.com/langlang/archive/2010/12/01/1893866.html 7z.exe 是 7-Zip 的命令行版本.7z.exe 使用 7 ...

  7. Basic Concepts of International Trade

    The international trade structure is a reflection of worldwide economic development, industrial stru ...

  8. C#笔记2:重构

    转: 最常用的重构指导 参考:http://www.cnblogs.com/KnightsWarrior/archive/2010/06/30/1767981.html,本文示例代码多来自此处: 参考 ...

  9. javascript_22_for_js控制div每五个换一行

    2. 3. css: <style type="text/css"> div{height: 50px; width: 50px; background: #f1161 ...

  10. mysql 的数据文件

    mysql的数据文件 由于mysql的数据文件结构主要跟mysql的存储引擎相关,这里不做过多解释,具体查看各个引擎章节的内容 .首先上一段小辉老师的教程; 在MySQL 中每一个数据库都会在定义好( ...