Linux tee命令详解

一、tee命令介绍

  在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令。tee命令用于读取标准输入的数据,并将其内容输出到文件中(文件不存在,则创建)。tee命令最基本的用法就是显示输出结果并且保存内容到文件中tee命令默认是覆盖写,可以使用-a选项来实现追加写。tee命令的格式为:

[root@localhost ~]# tee [选项] 文件名

其中选项如下表所示:

-a 追加写入文件,而非覆盖写入
-i 忽略中断信号

 【例1】使用free命令显示系统内存使用信息,并使用tee命令将信息保存到文件mem.txt中:

➜  test free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

➜  test free -h|tee mem.txt   # 把输出内容保存到文件中(默认是覆盖写)      #要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

➜  test cat mem.txt
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

tee mem.txt命令可以将输出内容写入到指定文件(mem.txt)中,tee也可以写入多个文件,多个文件使用空格分隔,如:tee mem1.txt mem2.txt mem3.txt

【例2】使用tee命令将用户的输入保存到文件中

➜  test tee teefile.txt   # 用tee命令来把用户输入保存到指定文件中
My name is bcy           # 等待用户输入内容
My name is bcy           # 输出数据,进行输出反馈
hello bcy
hello bcy
^C

➜  test cat teefile.txt      
My name is bcy
hello bcy

二、在已经存在的文件底部追加内容

  上节介绍了tee命令可以将输出写入到文件中,默认是覆盖写入。我们可以使用-a选项来追加写入

➜  test cat mem.txt
memory info as list:


➜  test free -h|tee -a mem.txt
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B


➜  test cat mem.txt
memory info as list:
              total        used        free      shared  buff/cache   available
Mem:            15G        2.1G        5.3G        159M        8.2G         13G
Swap:            0B          0B          0B

可以看到,使用-a追加写,并没有覆盖原有内容,推荐使用。

三、其他写入文件的方式

  Linux中经常会用到将内容输出到某文件当中,只需要在执行命令后面加上>或者>>号即可实现把内容写入到文件中。

  >:将一条命令的执行结果(标准输出,或者错误输出,本来都要打印到屏幕上面的)重定向其它输出设备,如文件文件不存在则创建,默认是覆盖写,>>是追加写

  >>:把命令的执行结果追加写入到文件中,文件不存在则创建

➜  test ll
total 28K
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  33 Sep 19 22:34 hello.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  25 Sep 20 17:46 teefile.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  25 Sep 13 10:53 temp.txt
-rw-r--r-- 1 baichunyu.bcy baichunyu.bcy  38 Sep 20 09:26 test.txt
➜ test ll
>a.txt # 覆盖写入a.txt,文件不存在,则创建 ➜ test cat a.txt # 成功写入 total 28K -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 0 Sep 21 17:30 a.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 13 10:53 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 38 Sep 20 09:26 test.txt ➜ test echo "my name is bcy">>a.txt # 向a.txt中追加写入 ➜ test cat a.txt # 写入成功 total 28K -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 0 Sep 21 17:30 a.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 13 10:53 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 38 Sep 20 09:26 test.txt my name is bcy ➜ test echo "what's your name?">a.txt # 向a.txt中覆盖写入 ➜ test cat a.txt # 覆盖原来的内容 what's your name?
原文地址:https://www.cnblogs.com/baichunyu/p/15314763.html