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