Redirecting Errors
2> and /dev/null.
Here's something surprising. Commands have two separate ways to print. One for normal output, and one for error messages. Both appear on your screen, so they look the same. But you can send them to different places.

- Two kinds of output
- Normal, and errors
- You can separate them
Three streams
Every command has three streams. Standard input, number zero, is what it reads. Standard output, number one, is its normal results. And standard error, number two, is for error messages. The greater than sign you already know, only redirects stream one.
Errors still appear
Watch this. We list a file that exists, and one that doesn't, and redirect the output into a file. The error still appears on screen, because it's on stream two. The file only received the good result.
ls notes.txt missing.txt > out.txt ls: cannot access 'missing.txt': No such file or directory cat out.txt notes.txt
Sending errors away
Two, greater than, redirects the errors. Here, into errors dot TXT. And slash dev slash null is a special file, that throws away anything you write into it. Like a black hole. Two greater than slash dev slash null silences errors completely. Ampersand greater than redirects both streams at once.
ls notes.txt missing.txt 2> errors.txt notes.txt cat errors.txt ls: cannot access 'missing.txt': No such file or directory ls notes.txt missing.txt 2> /dev/null notes.txt
Summary
Stream one is normal output, stream two is errors. Two greater than redirects errors, and slash dev slash null throws them away. Next, saving output and seeing it at the same time.
Key takeaways- 1 stdout, 2 stderr
- 2> errors.txt
- 2>/dev/null hides errors