linux 中 tee命令

发布时间 2023-06-22 21:37:36作者: 小鲨鱼2018

 

tee命令 :同时标准输出保存文件

 

001、

[root@PC1 test01]# ls
[root@PC1 test01]# seq 3 | tee a.txt     ## 标准输出的同时,保存文件
1
2
3
[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1
2
3

 

002、同时将文件复制多份

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt                             ## 测试文件
1
2
3
[root@PC1 test01]# cat a.txt | tee aaa bbb ccc ddd       ## 同时将文件复制多份
1
2
3
[root@PC1 test01]# ls
aaa  a.txt  bbb  ccc  ddd
[root@PC1 test01]# cat a.txt
1
2
3
[root@PC1 test01]# md5sum *                           ## 验证
c0710d6b4f15dfa88f600b0e6b624077  aaa
c0710d6b4f15dfa88f600b0e6b624077  a.txt
c0710d6b4f15dfa88f600b0e6b624077  bbb
c0710d6b4f15dfa88f600b0e6b624077  ccc
c0710d6b4f15dfa88f600b0e6b624077  ddd

 

003、将文件显示多次

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1
2
3
[root@PC1 test01]# cat a.txt | tee - -    ## 有几个横杠将显示 n + 11
2
3
1
2
3
1
2
3

 

004、-a参数表示追加

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1
2
3
[root@PC1 test01]# wc -l a.txt | tee xxx    ## 标准输出,并保存只文件
3 a.txt
[root@PC1 test01]# cat xxx
3 a.txt
[root@PC1 test01]# wc -l a.txt | tee -a xxx   ## 标准输出,并追加
3 a.txt
[root@PC1 test01]# cat xxx       ## 查看追加结果
3 a.txt
3 a.txt

 

005、利用tee命令来生成文件

a、tee + file

[root@PC1 test01]# ls
[root@PC1 test01]# tee a.txt       ## tee + file
a b c d                            ## 输入换行符后会标准输出
a b c d
0 1 2 3
0 1 2 3
^C
[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt       ## 查看结果
a b c d
0 1 2 3

 

b、 tee + file + 结束符

[root@PC1 test01]# ls
[root@PC1 test01]# tee a.txt << EOF    ## tee + file + 结束符
> 00 11 22 33
> aa bb cc dd
> EOF
00 11 22 33
aa bb cc dd
[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt           ## 查看结果文件
00 11 22 33
aa bb cc dd

 

参考:https://blog.csdn.net/carefree2005/article/details/121537513