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

用途说明

在执行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: *: 没有那个文件或目录
[root@web ~]#

Linux 重定向输出到多个文件中的更多相关文章

  1. Linux命令行批量替换多文件中的字符串【转】

    Linux命令行批量替换多文件中的字符串[转自百度文库] 一种是Mahuinan法,一种是Sumly法,一种是30T法分别如下: 一.Mahuinan法: 用sed命令可以批量替换多个文件中的字符串. ...

  2. linux sed 批量替换多个文件中的字符

    格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...

  3. Java基础面试操作题:读取该文件内容,并按照自然顺序排序后输出到 另一个文件中

    package com.swift; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...

  4. shell脚本 在后台执行de 命令 >> 文件 2>&1 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)

    命令 >> 文件 2>&1或命令 &>> 文件 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面) # ll >>aaa 2> ...

  5. [转载] linux查找目录下的所有文件中是否含有某个字符串

    链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...

  6. 把cmd信息中的正常和异常输出分别输出到不同txt文件中

    场景一: 1.大量滚动信息容纳不下,在小黑屏中被冲刷掉. 2.希望把正常输出和异常输出分别输出到不同地方. 相关命令 一共有4个输出到文件的命令,现以jar命令打war包举例说明: 命令 说明 举例  ...

  7. linux查找目录下的所有文件中是否含有某个字符串

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l ...

  8. linux查找目录下的所有文件中是否含有某个字符串 (转)

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...

  9. linux查找目录下的所有文件中是否含有某个字符串 <zhuan>

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...

随机推荐

  1. 小议linux

    小议linux在虚拟机上的安装和命令行 如何解决intel-vt-x被禁用而无法正常安装虚拟机 一般在新的电脑上第一次安装虚拟都可能出现如下图的问题:intel-vt-x(intel Virtuali ...

  2. cdh中hdfs非ha环境迁移Namenode与secondaryNamenode,从uc机器到阿里;

    1.停掉外部接入服务: 2 NameNode Metadata备份: 2.1 备份fsimage数据,(该操作适用HA和非HA的NameNode),使用如下命令进行备份: [root@cdh01 df ...

  3. dbtool一bug跟踪记

    注:这篇日志是好多年前,我还在从兴公司时写的.现在都从从兴公司离职很久了,从兴也没落了,可惜.看了一下,虽然出现了部分代码,但不至于泄漏什么机密,查bug过程的原理也有可以让新手借鉴的地方,就原文照搬 ...

  4. 【Jmeter测试】使用Java请求进行Dubbo接口的测试

    如何构建一个Dubbo接口测试的通用框架(https://github.com/nitibu/jmeter-dubbo-test)​从上面的流程我们可以看出,测试类大致的一个结构: 使用json文件来 ...

  5. loadrunner12安装教程

    全套五个文件: 独立安装包,插件包,LR安装包,语言包,版本说明书 loadrunner 12安装教程 1.首先下载Loadrunner12安装包.下载下来将会有四个安装包. HP_LoadRunne ...

  6. 搭建docker 私有镜像仓库

    前期准备 服务器:centos 7.3 docker-ce: 18.06.1-ce docker-compose: 1.22.0 docker 安装 首先,更新系统 yum update yum up ...

  7. Just a Hook:线段树+区间修改

    E - Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most ...

  8. Amazon.com Seller Distributed Inventory Placement Inventory Placement Service

    Greetings, Thank you for writing to us. I understand that you would like to send inventory to our wa ...

  9. rhel6 mysql skip-grant-tables 添加用户报错 ERROR 1290

    不小心把数据库密码忘掉了, 这个时候我们只需要在数据库的配置文件里面添加 skip-grant-tables 然后重新启动服务,再登录数据库就不要我们输入密码了 这个时候我成功登录数据,可是不小心又把 ...

  10. mariadb BINLOG_FORMAT = STATEMENT 异常

    当在mariadb中插入数据是报 InnoDB is limited to row-logging 异常: java.sql.SQLException: Cannot execute statemen ...