Linux Error Redirection
2> stderr
Command ‘2>’ redirects the error of an output.It helps us you to keep our display less messy by redirecting error messages.
Example:
Look at the above snapshot, by using command “zcho hyii 2> /dev/null” (here echo command is wrong), we didn’t get any error message. But when we use command “zcho hyii” error message is displayed in the terminal. Hence, ‘2>’ redirects the error message in the mentioned directory keeping your terminal error message free.
2>&1
This command helps in redirecting the stdout and stderr in the same file.
Example:
Look at the above snapshot, ‘abc.txt and error.txt’ is directing to the same file ‘newfile.txt’.
Note: Order of redirections is really important.
If you’ll write:
then, stdout and stderr both will be directed to the file dirlist.
But if you’ll write:
then, only stdout will be redirected to dirlist. This is because, before the stdout is redirected to dirlist, stderr has made a copy of stdout.