Redirect stderr to /dev/null

I am using the following command on a Unix server:

find . -type f -name "*.txt" | xargs grep -li 'needle'

Since grep -R is not available, I have to use this find/xargs solution. Every time a file cannot be opened, grep tells me this:

grep: can't open "foo.txt"

I want to get rid of this message, so I tried to redirect stderr to /dev/null, but somehow this is not working.

find . -type f -name "*.txt" | xargs grep -li 'needle' 2>/dev/null

I want to preserve stdout (i.e. write the results to the console), and only hide these grep error messages. Instead of 2>, I also tried &>, but this also did not work. How can I fix this?


Solution 1:

In order to redirect stderr to /dev/null use:

some_cmd 2>/dev/null

You don't need xargs here. (And you don't want it! since it performs word splitting)

Use find's exec option:

find . -type f -name "*.txt" -exec grep -li needle {} +

To suppress the error messages use the -s option of grep:

From man grep:

-s, --no-messages Suppress error messages about nonexistent or unreadable files.

which gives you:

find . -type f -name "*.txt" -exec grep -lis needle {} +

Solution 2:

Just move the redirection to the first command, i.e.

find ... 2>/dev/null | xargs ...

Or you can enclose everything in parenthesis:

(find ... | xargs ...) 2>/dev/null