Hom's Blog


bash:tee

tee命令如同其名, T , 将输出输出到屏幕的同时可以将输出再转到文件当中. 需要使用管道|来将输出递交给tee. 其功能主要有三:

输出到文件

会将输出定向到屏幕和文件. 会覆盖文件.

echo 'hello world' | tee hi.txt

可以配合echo使用, echo 的不换行 -n 也起效.

如果没有跟文件名, 就没有效果.

如果想把标准错误也重定向出来, 就要将标准错误定向到标准输出

ls "hehe.txt"
# ls: hehe.txt: No such file or directory

#### tee只接受标准输出, 不接受标准错误
ls "hehe.txt" | tee abc.txt
cat abc.txt
# 

#### 使用 2>&1 将错误重定向到标准输出, tee可以接收了. 
ls "hehe.txt" 2>&1 | tee abc.txt
cat abc.txt
# ls: hehe.txt: No such file or directory
追加到文件

使用 -a 选项实现追加(没有就创建).

echo 'hello lovely girl' | tee -a hi.txt

重复输出

使用 - 来实现重复输出, 一个-多重复输出一次. 例如 --- 就会输出四次一样的结果.用途不明…


echo "hello" | tee --

# hello
# hello
# hello


◆ 本文地址: http://platinhom.github.io/2016/01/12/bash-tee/, 转载请注明 ◆

前一篇: Linux下统计文件或文件夹数量
后一篇: Python:requests-cookies


Contact: Hom / 已阅读()
Source 类别: Coding  标签: Bash