Linux Error Redirection

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.


zcho hyii 2> /dev/null

Linux Error Redirection1

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.


This command helps in redirecting the stdout and stderr in the same file.


newfile.txt > abc.txt and error.txt 2>&1

Linux Error Redirection2

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:

ls > dirlist 2>&1 

then, stdout and stderr both will be directed to the file dirlist.

But if you'll write:

ls 2>&1 > dirlist

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.