linux - 如何将输出重定向到文件和标准输出

标签 linux bash file-io io stdout

在 bash 中,调用 foo 会在标准输出上显示该命令的任何输出。

调用 foo > output 会将来自该命令的任何输出重定向到指定的文件(在本例中为“output”)。

有没有办法将输出重定向到文件让它显示在标准输出上?

最佳答案

您想要的命令名为 tee :

foo | tee output.file

例如,如果您只关心标准输出:

ls -a | tee output.file

如果你想包含 stderr,请执行:

program [arguments...] 2>&1 | tee outfile

2>&1 将 channel 2(stderr/标准错误)重定向到 channel 1(stdout/标准输出),这样两者都写为 stdout。从 tee 命令开始,它也指向给定的输出文件。

此外,如果你想追加到日志文件,使用tee -a作为:

program [arguments...] 2>&1 | tee -a outfile

关于linux - 如何将输出重定向到文件和标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55369563/

相关文章:

linux - MQ 资源管理器 - 无法加载 SWT 库

linux - 如何理解哈希wget?

c - c语言中如何打开子目录下的文件

linux - Maven 构建使用/分配大量内存

c - Facebook拼图提交失败

python - 在Python中执行类似于Linux中的curl的URL - JenkinsAPI

linux - 终端 - 如何运行 HTTP 请求 'PUT'

bash - Git Hook : '.git/hooks/pre-commit' : Operation not permitted

c - 如何将使用execl运行的进程的输出写入c中的文件

c - 用`scanf()`读取逗号分隔的输入