Tips & Tricks


It so happens mostly that standard commands/programs/services stream normal log messages to stdout while error log messages to stderr stream. Now, if you just do something like :

$ [some-command] > logfile

Then only the messages that were directed to stdout would be redirected to the file ‘logfile’ but no message that was directed to stderr would be redirected to the file.

Here is an example :

$ touch new > /home/YourName/practice/logfile
touch: cannot touch `new’: Permission denied

$ cat /home/YourName/practice/logfile
$

So we see that error was not redirected to the log file.

Now, to correct this, do something like :

$ touch new > /home/YourName/practice/logfile 2>&1

$ cat /home/YourName/practice/logfile
touch: cannot touch `new’: Permission denied

So we see that this time the error was redirected to the file successfully. Please note that 2>&1 combines both stdout and stderr streams to stdout stream only.

Go Back to Menu