2011-02-11

Bash - How to output all (stdout & stderr) into log file in Bash

Purpose :
When scripting Bash scripts, often we'll need to output all stdout & stderr of a particular command into a log file, for logging purposes or diagnostic purposes.






Command :
&>
(Yup, that's the only 2 symbol to achieve the purpose)

Example :
  • This will clear the current file (if exist) and output everything to the file :
    tar zcf somefile.tar.gz /etc/ &> log.file
  • This will "append" the output into the file :
    tar zcf somefile.tar.gz /etc/ &>> log.file
    *** Do take note of the double ">>" symbol

Farvel !!!

No comments: