Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect output to a file and stdout

In bash, calling foo would display any output from that command on the stdout.

Calling foo > output would redirect any output from that command to the file specified (in this case 'output').

Is there a way to redirect output to a file and have it display on stdout?

like image 541
SCdF Avatar asked Jan 07 '09 01:01

SCdF


People also ask

How do I redirect output from stdout to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How would you redirect output from stdout to a file in python?

stdout = original print('This string goes to stdout, NOT the file! ') if __name__ == '__main__':Redirecting stdout / stderr redirect_to_file('Python rocks! ') Here we just import Python's sys module and create a function that we can pass strings that we want to have redirected to a file.

How do you direct output to a file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


1 Answers

The command you want is named tee:

foo | tee output.file 

For example, if you only care about stdout:

ls -a | tee output.file 

If you want to include stderr, do:

program [arguments...] 2>&1 | tee outfile 

2>&1 redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee command.

Furthermore, if you want to append to the log file, use tee -a as:

program [arguments...] 2>&1 | tee -a outfile 
like image 157
Zoredache Avatar answered Oct 02 '22 00:10

Zoredache