Search code examples
linuxbashgreppipesh

Redirect stderr to /dev/null for two piped commands: find | grep


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

  • 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 {} +