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 ?

2
  • Is only about apt-get right?
    – Braiam
    Jun 20, 2014 at 20:52
  • @Braiam I think the solution I accepted can be applied to all other commands, do you think so ?
    – user284234
    Jun 21, 2014 at 8:32

5 Answers 5

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.

6
  • I'd add "2>&1" before the "|" Jun 20, 2014 at 8:34
  • 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, 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.
    – Darkhogg
    Jun 20, 2014 at 23:05
  • 1
    you could use |& instead of 2>&1 | in bash
    – jfs
    Jun 25, 2014 at 9: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
9
  • script sends the command output to a file, but does not show it on the screen.
    – Aaron
    Jun 19, 2014 at 13:18
  • 2
    @BryceAtNetwork23. Did you try the command ? script does send output to screen.
    – exore
    Jun 19, 2014 at 13:26
  • 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, 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 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, 2014 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. Jun 19, 2014 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
1
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.

1

try tee command. You can found here some examples.

Simple usage:

  <command> | tee file

example:

  sudo apt-get install whatYouWant | tee outputFile
0

You must log in to answer this question.