Redirect stderr to /dev/null

LinuxGrep

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionr0f1View Question on Stackoverflow
Solution 1 - Linuxhek2mglView Answer on Stackoverflow
Solution 2 - LinuxcslView Answer on Stackoverflow