用途说明
   在执行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. ros-Qt代码环境的搭建

    1 建立package catkin_create_pkg beginner_tutorials roscpp 2 导入Qt Qt中打开整个工作空间的src/CMakeLists.txt 在倒数第二行 ...

  2. WPF学习01:初始XAML浅析

    本文内容: 浅析WPF应用默认创建的XAML中元素.attributes. 新建WPF工程“HelloWPF”. 初始创建的主窗体XAML代码如下: <Window x:Class=" ...

  3. Cisco IOS Basic CLI Configuration:Access Security 01

    1.  Telnet Switch Config: Switch>en Switch#conf t Enter configuration commands, one per line.  En ...

  4. swap分区添加

    首先你需要使用命令:dd 来创建一个swapfile,然后你需要使用mkswap命令在设备或者文件中创建一个Linux swap分区a) 使用root用户登陆b) 使用下面的命令创建一个2G的 Swa ...

  5. [转]adb pull Permission denied及no such file错误

    adb pull  Permission denied及no such file错误 http://www.the8m.com/blog/article/javadk/adbpull.html XP系 ...

  6. Careercup - Facebook面试题 - 5412018236424192

    2014-05-01 01:32 题目链接 原题: Given a linked list where apart from the next pointer, every node also has ...

  7. linux 删除某种规则命名的文件

    由于android开发需要删除以IMG_开头命名的图片文件,因此用到此命令 命令格式: rm IMG_*

  8. as3.0服务端FMS软件常用的方法与属性参考示例

    转自:http://www.cuplayer.com/player/PlayerCode/RTMP/2012/0918429.html Application类的方法汇总方法 描述Applicatio ...

  9. 处理一则MySQL Slave环境出现ERROR 1201 (HY000): Could not initialize master info structure的案例

    mysql> start slave; ERROR (HY000): Slave failed to initialize relay log info structure from the r ...

  10. c++ memset 函数 及 坑

    #include <string.h> #include <stdio.h> typedef struct ss{ int num; ][]; }tent; tent a; i ...