AI Hints
Home›Linux Tutorial›Errors and Combining
Linux · Lesson 52 of 120

Redirecting Errors

2> and /dev/null.

⏱ 2 min read▶ Video at 1:06:46🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

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.

Redirecting Errors
From the video: Redirecting Errors
  • 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.

0 stdinwhat goes in
1 stdoutnormal output
2 stderrerror messages
>redirects stream 1 only

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.

Terminal
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.

Terminal
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
Next up: tee — save and see