Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print STDOUT/STDERR and write them to a file in Bash?

Is there a way to have Bash redirect STDOUT/STDERR to a file yet still print them out to the terminal as well?

like image 304
Naftuli Kay Avatar asked Jan 19 '11 18:01

Naftuli Kay


2 Answers

This will redirect both STDOUT and STDERR to the same file:

some_command 2>&1 | tee file.log

Example

$ touch foo; ls foo asfdsafsadf 2>&1 | tee file.log
ls: asfdsafsadf: No such file or directory
foo
$ cat file.log
ls: asfdsafsadf: No such file or directory
foo
like image 88
SiegeX Avatar answered Oct 18 '22 06:10

SiegeX


Use the tee command.

$ echo "hi" | tee output.txt
hi
[unix]$ ls
output.txt
[unix]$ cat output.txt 
hi
like image 23
I82Much Avatar answered Oct 18 '22 05:10

I82Much