当前位置: 首页>>技术教程>>正文


如何既在控制台上显示命令行的输出又将输出保存到文本文件中?

,

问题描述

如何通过同时查看安装过程(对于我来说很长)并将其输出保存到文本文件中来同时运行sudo apt-get install

最佳方法

使用script命令。它将复制屏幕上显示的所有内容

script -c "sudo apt-get install things" script-file.script

次佳方法

您可以使用tee命令来完成此操作。

sudo apt-get install someapp 2>&1 | tee ~/someappInstall.txt

Look here for more info或执行man tee

注意:正如其他人提到的那样,需要2>&1将STDERR重定向到STDOUT以捕获任何错误。有关2>&1实际功能的详细说明,请参见this StackOverflow question

第三种方法

tee将按要求完成该工作。

要将输出捕获到文件中,请使用:

sudo apt-get install your_software | tee log_file.txt

这将仅捕获输出,而不捕获任何错误消息。如果您还想记录错误消息,请将命令修改为:

sudo apt-get install your_software 2>&1  | tee log_file.txt

第四种方法

apt-get(通常是APT)的优点之一是,它们在/var/log/apt中存储几乎所有内容的日志文件,甚至包括您运行低谷的任何命令的终端输出。例如,这是我的/var/log/apt/term.log中的最后一个条目:

Log started: 2014-06-20  16:46:08
(Reading database ... 252472 files and directories currently installed.)
Removing xdotool (1:3.20130111.1-3.1) ...
Processing triggers for man-db (2.6.7.1-1) ...
Log ended: 2014-06-20  16:46:33

现在,与实际输出进行比较:

➜  ~  sudo apt-get remove xdotool 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libxdo3
Use 'apt-get autoremove' to remove it.
The following packages will be REMOVED:
  xdotool
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
After this operation, 135 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 252472 files and directories currently installed.)
Removing xdotool (1:3.20130111.1-3.1) ...
Processing triggers for man-db (2.6.7.1-1) ...

它为我节省了在大多数情况下不相关的几行,并且自动执行。因此,您不需要任何额外的命令即可执行所需的操作,apt-get会为您完成此操作。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/10592.html,未经允许,请勿转载。