How to both display a command line's output on console and save the output into a text file?

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 ?


Solution 1:

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.

Solution 2:

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

Solution 3:

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