用途说明
   在执行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. C#中泛型和单链表

      泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类 ...

  2. WCF + EF 遇到的问题

    此文承接上一文章 基于WCF的API实现 http://www.cnblogs.com/heyixiaoran/p/4000695.html 由于上一次Entity部分没怎么写,到Services里识 ...

  3. oracle作业

    http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍

  4. lnmp停用nginx,改用apache

    编译安装的lnmp环境 总是出现502错误,修改了各种配置也没用,暂时先放弃nginx,改用apache apache使用yum安装方式 需要注意的事项,将网站根目录的用户组改为 chown apac ...

  5. 关于php框架

    最近学习yii2.0 强哥原班人马开发,不得不看 同时也需要关注一下drupal8和symfony2

  6. EntityFramework中的datetime2异常的解决

    (转)   最近使用.net的Entity Framework构建网站数据层,给一个实体的DATETIME类型的属性赋值时 突然莫名奇妙显示有一个类型不匹配的异常如下: System.Data.Sql ...

  7. UIWebView 需改userAgent 并且加载微信公共账号

    需要注意的是需要获取原来的UIWebView的User-Agent,然后拼接上自己新的User-Agent,貌似直接替换原来的无效,另外,修改User-Agent之后重新创建UIWebView加载网页 ...

  8. 特征值分解,奇异值分解(SVD)

    特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. 1. 特征值: 如果说一个向 ...

  9. 无废话网页重构系列——(7)布局(区块、栅格)、模块组件(module)

    本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4654800.html 在构建HTML主干结构后,开始编写“页面布局”和“模块组件”: 页面框架由几个主干结 ...

  10. [转载]JS中如何定义全局变量

    三种方法 1.在js的function外定义一个变量 var name='测试'; function XX(){        alert(name); } 2.不使用var,直接给定义变量,隐式的声 ...