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
script
since the command doesn't have to be quoted. So advantages like bash completion is easier to accomplish.– DanJun 20, 2014 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) Jun 20, 2014 at 11:46 -
2@Dan + works with functions, aliases, builtins and even command groups and subshells. This should be the accepted answer IMO.– DarkhoggJun 20, 2014 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
-
script
sends the command output to a file, but does not show it on the screen.– AaronJun 19, 2014 at 13:18 -
2@BryceAtNetwork23. Did you try the command ? script does send output to screen.– exoreJun 19, 2014 at 13:26
-
1I did. I ran
script -c "history" ~/hist.txt
and saw output indicatingScript started
andScript done
, but I didn't see output from the actual command on the screen.– AaronJun 19, 2014 at 13:30 -
10@BryceAtNetwork23
history
is 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 commandhistory
via 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.– exoreJun 19, 2014 at 13:42 -
1
script
can also be run interactively. Just typescript
at the command prompt. You will get a new shell, and any commands you type will have their output saved. Typeexit
to end the shell and save the file. By default, the output is calledtypescript
and it will contain everything shown on your screen, whether you typed it, or it was the output of a command. Thehistory
command should still be available in the new shell as well. Jun 19, 2014 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
bash
supports the|&
operator,sudo apt-get install your_software |& tee log_file.txt
will pipe both stdout and stderr totee
. (Kudos to J.F. Sebastian who suggested this elsewhere.) Sep 11, 2014 at 7: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.
apt-get
right?