Redirect stderr to /dev/null
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
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 {} +