Error Redirection
Command output is sometimes mixed up with command errors since they are both sent to the screen by default. Run the following command
cp
What did you get displayed?
Is that output or error? Now run the command. cp> cpfile What happened? Since the same message got displayed on the screen and was not sent to file cpfile then it must not be output.It is error. To understand how to redirect errors, we should learn about file descriptors. There are three file descriptors used by programs to specify input, output, and error. Standard input has file descriptor 0 Standard output has file descriptor 1 Standard error has file descriptor 2
There is no need to use the file descriptors 0 and 1 when redirecting input and output respectively since they use two different characters namely < and > To redirect error we need to use the (>) character so to distinguish it from redirecting error, we must specify the file descriptor before the > character as follows:
What happened now?
Check the contents of file cpfile. What did you find?
Redirecting output and error to different places may be very useful especially when dealing with commands that produce both at the same time. Try the following command:
What did you get? Was that output or error?
Now run the command as follows:
What did you get now?
Check file errors content. Now run the command as follows.
What happened?
Check both files output and error.
To append errors use (2>>).