This week, I am redirecting standard errors. First, to understand standard errors, we need to know that the linux shell refers to standard input, output, and error as 0, 1, and 2 respectively as their 'file descriptors'. So, by using the number 2, we can redirect an error like this: la -l /bin/usr (bin/usr does not exist which is why we'll get an error) 2> ls-error.txt. That number 2 tells the shell to redirect errors to ls-error.txt.
Now say you want to redirect both standard outputs and errors to the same file. Do this: ls -l /bin/usr > ls-output.txt 2>&1. This moves the outputs to the ls-output.txt then redirects errors to 1 (which is the file descriptor for outputs. Remember to put the error redirection at the end of the command, or it won't work. That's the old way. There is a new way to redirect both to the same file. Try this out: ls -l /bin/usr &> ls-output.txt. Less typing = good!
Do you hate error messages? Me too! So lets just throw them away. I don't want to see it on my screen, so I can type this command that probably sounds familiar to some and is quite the joke to others: ls -l /bin/usr 2> /dev/null. /dev/null is a universally known concept in Unix that tosses all those bits into a bucket and does absolutely nothing with them.
Next time on HakTip, let's have some fun with cats! Tell me about your ideas in the comments or email me email@example.com with your thoughts. And be sure to check out our sister show, Hak5 for more great stuff just like this. I'll be there, reminding you to trust your technolust.