25

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 Answers5

52

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.

Aaron
  • 6,714
  • I'd add "2>&1" before the "|" – Olivier Dulac Jun 20 '14 at 08:34
  • Good call. Edit made. – Aaron Jun 20 '14 at 10:27
  • 2
    This is more practical than script since 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
    you could use |& instead of 2>&1 | in bash – jfs Jun 25 '14 at 09:30
17

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
exore
  • 997
  • 6
  • 10
  • script sends 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, tee won't redirect stderr to the file, whereas script will. The formating of tee is nicer though (on my laptop, script added lots of un-necessary newlines) – Aserre Jun 19 '14 at 13:30
  • 1
    I did. I ran script -c "history" ~/hist.txt and saw output indicating Script started and Script 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 script with apt-get install somepackage and it worked fine, but with history, 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 update and it worked just fine...but not with history? Wow. @Exore, good answer...+1 from me. – Aaron Jun 19 '14 at 13:36
  • 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 command history 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. – exore Jun 19 '14 at 13:42
  • 1
    script can also be run interactively. Just type script at the command prompt. You will get a new shell, and any commands you type will have their output saved. Type exit to end the shell and save the file. By default, the output is called typescript and it will contain everything shown on your screen, whether you typed it, or it was the output of a command. The history command should still be available in the new shell as well. – Brian Minton Jun 19 '14 at 19:36
15

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
user.dz
  • 48,105
Alex C
  • 300
  • 1
  • 4
9

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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
1

try tee command. You can found here some examples.

Simple usage:

  <command> | tee file

example:

  sudo apt-get install whatYouWant | tee outputFile
Lety
  • 6,039
  • 2
  • 29
  • 37