Monday, January 13, 2014

How to redirect output of a running process to a file and how to overwrite a file

I am writing this when i had a need to redirect the output of a running process to a file. For example in the case of output redirection

 airodump-ng mon0 > file.txt  

this won't work. The reason behind it is that usually a process may send output to standard output or standard error. In general case former is for information and latter is for errors. In certain cases both may get mixed up. In the above case standard error is used and we need to pipe it to the file.
It can be done by

 airodump-ng mon0 > file.txt 2>&1  

This says to send standard ouptut to file file.txt and reroute 2 (which is file id for standard error) into 1 (file id for standard output).
Over writing

By default when a running process is piped to a file, the output gets appended at each instant when a new output (stdout or stderr) is generated. But if we want to overwrite the previous output we can use,  the following code to do so

  airodump-ng mon0 &> file.txt 2>&1   

Tags:- output-redirection,piping, bash, shell, process, overwriting,file-overwriting

No comments:

Post a Comment