如何将终端输出保存到文件?
如何将命令的输出保存到文件?
有没有办法不使用任何软件? 我想知道怎么做。
是的,只需将输出重定向到文件:
SomeCommand > SomeFile.txt
或者,如果要附加数据:
SomeCommand >> SomeFile.txt
如果你想要stderr
也使用这个:
SomeCommand &> SomeFile.txt
或者这个附加:
SomeCommand &>> SomeFile.txt
如果你想在控制台和文件中同时显示 stderr
和输出,请使用:
SomeCommand 2>&1 | tee SomeFile.txt
(如果只想输出,请删除上面的2
)
要将命令的输出写入文件,基本上有10种常用方法。
概述:
请注意,语法列中的
ne
表示“不存在”。
有一种方法,但它太复杂,不适合列。 您可以在列表部分找到有关它的有用链接。
|| visible in terminal || visible in file || existing Syntax || StdOut | StdErr || StdOut | StdErr || file ==========++==========+==========++==========+==========++=========== > || no | yes || yes | no || overwrite >> || no | yes || yes | no || append || | || | || 2> || yes | no || no | yes || overwrite 2>> || yes | no || no | yes || append || | || | || &> || no | no || yes | yes || overwrite &>> || no | no || yes | yes || append || | || | || | tee || yes | yes || yes | no || overwrite | tee -a || yes | yes || yes | no || append || | || | || ne (*) || yes | yes || no | yes || overwrite ne (*) || yes | yes || no | yes || append || | || | || |& tee || yes | yes || yes | yes || overwrite |& tee -a || yes | yes || yes | yes || append
列表:
-
command > output.txt
标准输出流将仅重定向到文件,在终端中不可见。 如果该文件已存在,则会被覆盖。
-
command >> output.txt
标准输出流将仅重定向到文件,在终端中不可见。 如果文件已存在,则新数据将附加到文件末尾。
-
command 2> output.txt
标准错误流将仅重定向到文件,在终端中不可见。 如果该文件已存在,则会被覆盖。
-
command 2>> output.txt
标准错误流将仅重定向到文件,在终端中不可见。 如果文件已存在,则新数据将附加到文件末尾。
-
command &> output.txt
标准输出和标准错误流都将仅重定向到文件,终端中不会显示任何内容。 如果该文件已存在,则会被覆盖。
-
command &>> output.txt
标准输出和标准错误流都将仅重定向到文件,终端中不会显示任何内容。 如果该文件已存在,则新数据将附加到文件末尾。
-
command | tee output.txt
标准输出流将被复制到文件中,它仍然可以在终端中看到。 如果该文件已存在,则会被覆盖。
-
command | tee -a output.txt
标准输出流将被复制到文件中,它仍然可以在终端中看到。 如果文件已存在,则新数据将附加到文件末尾。
-
(*)
Bash没有简写语法,只允许StdErr管道到第二个命令,这里需要再次与
tee
一起完成表格。 如果你真的需要这样的东西,请看“如何管道stderr,而不是stdout?” 有关如何通过交换流或使用进程替换来完成此操作的Stack Overflow 。 -
command |& tee output.txt
标准输出和标准错误流都将被复制到文件中,同时仍在终端中可见。 如果该文件已存在,则会被覆盖。
-
command |& tee -a output.txt
标准输出和标准错误流都将被复制到文件中,同时仍在终端中可见。 如果文件已存在,则新数据将附加到文件末尾。
您还可以使用tee
将输出发送到文件:
command | tee ~/outputfile.txt
稍作修改也会抓住stderr:
command 2>&1 | tee ~/outputfile.txt
或稍短且不太复杂:
command |& tee ~/outputfile.txt
如果您希望能够在实时查看命令输出的同时捕获命令输出,则 tee
非常有用。
您可以将命令输出重定向到文件:
your_command >/path/to/file
要将命令输出附加到文件而不是覆盖它,请使用:
your_command >>/path/to/file
需要考虑的改进 –
各种脚本会将颜色代码注入输出中,您可能不希望混乱日志文件。
要解决此问题,您可以使用程序sed删除这些代码。 例:
command 2>&1 | sed -r 's/'$(echo -e "\033")'\[[0-9]{1,2}(;([0-9]{1,2})?)?[mK]//g' | tee ~/outputfile.txt
对于cron
作业等,您希望避免使用Bash扩展。 等效的POSIX sh
重定向运算符是
Bash POSIX -------------- -------------- foo &> bar foo >bar 2>&1 foo &>> bar foo >>bar 2>&1 foo |& bar foo 2>&1 | bar
您会注意到POSIX工具在某种意义上更简单,更直接。 &>
语法是从csh
借用的,它应该已经说服你这是个坏主意。
some_command | tee command.log
some_command | tee command.log
和some_command > command.log
存在一个问题,即它们不会将命令输出实时保存到command.log
文件中。
要避免该问题并实时保存命令输出,您可以附加expect
包附带的unbuffer
。
例:
sudo apt-get install expect unbuffer some_command | tee command.log unbuffer some_command > command.log
假设log.py
包含:
import time print('testing') time.sleep(100) # sleeping for 100 seconds
你可以运行unbuffer python log.py | tee command.log
unbuffer python log.py | tee command.log
或unbuffer python log.py > command.log
更多信息: 如何实时将命令输出保存到文件?