如何将终端输出保存到文件?

如何将命令的输出保存到文件中?

有没有办法不使用任何软件? 我想知道怎么做。

是的,这是可能的,只需重定向输出(又名 stdout)到文件:

SomeCommand > SomeFile.txt  

或者如果你想追加数据:

SomeCommand >> SomeFile.txt

如果你想 stderr 以及使用这个:

SomeCommand &> SomeFile.txt  

或者这个追加:

SomeCommand &>> SomeFile.txt  

如果你想两者兼而有之 stderr 和输出 显示在控制台上 在文件中 用这个:

SomeCommand 2>&1 | tee SomeFile.txt

(如果您只想要输出,请删除 2 以上)

要将命令的输出写入文件,基本上有10种常用的方法。

概览:

请注意, n.e. 在语法列中表示"不存在"。
有一种方法,但它太复杂了,不适合列。 您可以在列表部分找到有关它的有用链接。

          || 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          ||          |          ||          |          || n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite n.e. (*) ||   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?"在堆栈溢出 在某些方面如何做到这一点,例如通过交换流或使用进程替换。

  • 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

这里有两个不同的问题。 第一个是在标题中:

如何将终端输出保存到文件?

第二个问题是在正文中:

如何将命令的输出保存到文件中?

这里发布的所有答案都解决了第二个问题,但没有一个解决了第一个问题,其中有一个很好的答案。 Unix和Linux:

这个答案使用一个鲜为人知的命令,称为 script 它将所有shell的输出保存到文本文件,直到您键入 exit. 命令输出仍然出现在屏幕上,但 也出现在文本文件中.

这个过程很简单。 使用方法:

$ script ~/outputfile.txtScript started, file is /home/rick/outputfile.txt$ command1$ command2$ command3$ exitexitScript done, file is /home/rick/outputfile.txt

然后看看你的命令1,2和3的记录输出:

cat ~/outputfile.txt

这类似于 较早的答案 的:

command |& tee ~/outputfile.txt
  • 但你不必使用 |& tee ~/outputfile.txt 每个后 commnd.
  • script 命令增加了重新加载的好处(或缺点) ~/.bashrc 当它开始的时候。
  • script command显示命令提示符($PS1)后跟您输入的命令。
  • script 命令以全彩色记录所有细节。

cron 工作等你想避免Bash扩展。 等效POSIX sh 重定向运算符是

Bash          POSIX------------  --------------foo &> bar    foo >bar 2>&1foo &>> bar   foo >>bar 2>&1foo |& bar    foo 2>&1 | bar

您会注意到POSIX设施在某种意义上更简单,更直接。 该 &> 语法是从借来的 csh 这应该已经让你相信这是个坏主意。

some_command | tee command.logsome_command > command.log 有问题,他们不保存命令输出到 command.log 实时文件。

为了避免这个问题并实时保存命令输出,您可以追加 unbuffer,其中带有 expect 包裹。


例子::

sudo apt-get install expectunbuffer some_command | tee command.logunbuffer some_command > command.log

假设 log.py 包含:

import timeprint('testing')time.sleep(100) # sleeping for 100 seconds

你可以跑 unbuffer python log.py | tee command.logunbuffer python log.py > command.log

更多资料: 如何将命令输出实时保存到文件?

如果要在运行命令时输出到文件:

script -c ./path/to/executable.bash -f log.txt