Error redirection with 2>
Many command-line programs that have a lot of expected output will also output occasional (expected) errors – think of a find
command that encounters occasional ‘permission denied’ errors for directories you’re not allowed to peek inside.
Although these kinds of errors are minor and expected, you don’t want them mixed in with everything else, polluting your output. This becomes especially important when you’re not using command-line tools interactively, but rather writing small scripts or larger programs that process the output of the commands you’re running.
You’ve seen how to redirect Standard Input (fd 0) and Standard Output (fd 1). Let’s look at how to redirect Standard Error (fd 2) using the 2>
(redirect file descriptor 2) syntax.
find /etc/ -name php.ini > /tmp/phpinis.log 2>/dev/null
This command searches for any files named php.ini
inside the /etc
directory tree. The files it...