tee
命令在 Linux 中用于读取标准输入数据并将其写入到标准输出和文件中。它常用于同时在终端显示输出和将输出保存到文件中。
以下是 tee
命令的一些常用用法:
- 将标准输入写入文件:
这将把字符串 "Hello, World!" 输出到 filename.txt
文件,并在终端显示相同的字符串。
- 同时写入多个文件:
这将把输出同时写入 file1.txt
和 file2.txt
,并在终端显示。
- 追加到文件而不是覆盖:
使用 -a
选项将内容追加到 file1.txt
文件末尾,而不是覆盖原有内容。
- 使用
-i
选项忽略中断:
使用 -i
选项时,tee
会在文件写入时忽略中断信号。
- 禁止输出到标准输出(屏幕):
将输出重定向到 /dev/null
可以禁止输出显示在终端。
- 结合管道和文件输出:
这将把 some_command
的输出写入 result.txt
,并通过管道传递给 other_command
。
- 使用
-p
选项打印输出:
使用 -p
选项会打印输出到标准输出,即使没有其他输出。
- 使用
-s
选项静默执行:
使用 -s
选项可以禁止 tee
在每次写入文件后刷新输出缓冲区。
tee
命令在需要记录命令输出或将输出同时发送到多个目的地时非常有用。它特别适合在脚本中使用,以确保关键信息被记录和显示。