用途说明
   在执行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. ASP.NET Core 1.0

    .NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...

  2. python之递归

    递归的定义:即对自己自身内容的引用. 有用的递归函数应包含以下几步份: 当函数直接返回值时有基本的实例(最小可能性问题): 递归实例,包括一个或者多个问题较小部分的递归调用: 递归的关键就是将问题分解 ...

  3. hdu 5690 2016"百度之星" - 初赛(Astar Round2A) All X 快速二次幂 || 寻找周期

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5690 题意:m个数字全为x mod k ?= c;其中m <= 1010,0 < c,k ...

  4. libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64

    版本:5.7.9 新装的CentOS 6.3 安装MySQL 5.7.9 出现的问题 1.首先卸载系统自带的mysql 5.1的包    yum   -y  remove   mysql-libs-5 ...

  5. SSHDroid(SSH Server for Android)通过PC或命令连接android

    1.下载berserker.android.apps.sshdroid.apk .(如果你懒的下载,给我留言,我会发给你) 2.安装到手机,显示如图: 简单解释一下:一般android系统没有root ...

  6. iOS常见问题(5)

    一.注意将之前storyboard中控制器删除之后,拖入一个新的控制器的时候,stroyboard中控制器的class也要重新填入自己想要展示的控制器,告诉stroyboard去加载哪个控制器. 二. ...

  7. 微软职位内部推荐-SDE2 (Windows driver)

    微软近期Open的职位: SDE2 (Windows driver) Job title: Software Development Engineer 2 Location: Shanghai, Ch ...

  8. C# WPF打印报表

    前天我的一个同学由于打印报表而苦恼,所以就介绍了一下WPF的打印报表,希望能帮助到大家. 展示报表 1. 首先新建项“报表”,选定项目,右击,点击“添加”->“新建项”->“报表”

  9. p1205单词翻转-递归解决

    题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转过来 输入描述 Input Description 输入包括一个英语句子. 输出描述 Output Descripti ...

  10. Codeforces Round #347 (Div. 2) B. Rebus

    题目链接: http://codeforces.com/contest/664/problem/B 题意: 给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立 题解: 贪 ...