How can I run sudo apt-get install by BOTH seeing the process of installation (long in my case) and saving its output into a text file ?
5 Answers
You can use the tee command to accomplish this.
sudo apt-get install someapp 2>&1 | tee ~/someappInstall.txt
Look here for more info, or execute man tee
Note: As others have mentioned, 2>&1 is necessary to redirect STDERR to STDOUT to catch any errors. See this StackOverflow question for a good explanation of what 2>&1 actually does.
-
-
-
2This is more practical than
scriptsince the command doesn't have to be quoted. So advantages like bash completion is easier to accomplish. – Dan Jun 20 '14 at 10:49 -
2@Dan: script doesn't need the
-c "something ...": if will then drop you to a shell, and finished when you exit that shell. Allows multiple commands, etc. Plus it keeps more "formatting" infos, allowing replaying of some more things (like : clear screen, etc) (but that also can mess up the output... ymmv) – Olivier Dulac Jun 20 '14 at 11:46 -
2@Dan + works with functions, aliases, builtins and even command groups and subshells. This should be the accepted answer IMO. – Darkhogg Jun 20 '14 at 23:05
-
1
use the script command. It will copy everything that goes to screen in a file
script -c "sudo apt-get install things" script-file.script
- 997
- 6
- 10
-
scriptsends the command output to a file, but does not show it on the screen. – Aaron Jun 19 '14 at 13:18 -
2@BryceAtNetwork23. Did you try the command ? script does send output to screen. – exore Jun 19 '14 at 13:26
-
Furthermore,
teewon't redirectstderrto the file, whereasscriptwill. The formating ofteeis nicer though (on my laptop,scriptadded lots of un-necessary newlines) – Aserre Jun 19 '14 at 13:30 -
1I did. I ran
script -c "history" ~/hist.txtand saw output indicatingScript startedandScript done, but I didn't see output from the actual command on the screen. – Aaron Jun 19 '14 at 13:30 -
I tested this solution before I accepted it: it really shows the output to the screen and saves it also to a text file. It is a simple a command and nothing additional to install. – Jun 19 '14 at 13:31
-
Well that is weird. I tested
scriptwithapt-get install somepackageand it worked fine, but withhistory, both my shell and my output file were blank – Aserre Jun 19 '14 at 13:34 -
That is weird. I also just tested it with
apt-get updateand it worked just fine...but not withhistory? Wow. @Exore, good answer...+1 from me. – Aaron Jun 19 '14 at 13:36 -
10@BryceAtNetwork23
historyis a shell builtint, not an external command. What happens is : 1) script starts, 2) script searches the history command, does not find it, so it guesses it should run this via a shell. 3) script runs the commandhistoryvia a shell 4) a new shell starts and executes the history internal command. Since this is a new non interactive shell, history has nothing to say. – exore Jun 19 '14 at 13:42 -
1
scriptcan also be run interactively. Just typescriptat the command prompt. You will get a new shell, and any commands you type will have their output saved. Typeexitto end the shell and save the file. By default, the output is calledtypescriptand it will contain everything shown on your screen, whether you typed it, or it was the output of a command. Thehistorycommand should still be available in the new shell as well. – Brian Minton Jun 19 '14 at 19:36
tee will do the job as required.
To capture the output into a file, use:
sudo apt-get install your_software | tee log_file.txt
This will only capture the output, but not any error messages. If you would also like to record error messages, modify the command to be:
sudo apt-get install your_software 2>&1 | tee log_file.txt
-
Or, since
bashsupports the|&operator,sudo apt-get install your_software |& tee log_file.txtwill pipe both stdout and stderr totee. (Kudos to J.F. Sebastian who suggested this elsewhere.) – Eliah Kagan Sep 11 '14 at 07:24
One of the beauty of apt-get (and APT in general) is that they store log files for almost everything, even the terminal output of any command that you run trough, in the /var/log/apt. For example, this is the last entry in my /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
Now, comparing with the actual output:
➜ ~ 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) ...
It saved me several lines that are not relevant in most cases, and it does automatically. So, you don't need any extra command to do what you want to do, apt-get does it for you.
- 67,791
- 32
- 179
- 269
apt-getright? – Braiam Jun 20 '14 at 20:52