Mac OS X find/grep generating “unknown –devices option”

findgreposx

I am entering a command I have done often with Linux, Unix perfectly fine but with Apple Mac OS X 10.8 (and probably before) I get a grep: unknown --devices option when I attempt to run the following command:

find . -type f -name '*.sql' 2>/dev/null | xargs grep -i 'texttolookfor'

I checked the results of the find command and they all appear to be just standard .sql files. And I should add that the problem occurs with other file searches not just .sql files. In searching the Apple site and Google I cannot seem to find any indication of what is going on here.

Does anyone have a suggestion?

Best Answer

It sounds like you've hit a file with a funny name, and xargs is treating it as two files. The best approach is to rework your find to deal with all names safely:

find . -type f -name '*.sql' -exec grep -i 'texttolookfor' '{}' +

This uses the find --exec COMMAND + syntax instead of xargs. You could also use -print0/xargs -0 (if that works on OS X, not sure), but there isn't really a reason to, unless you need other xargs features.

Finally, if OS X grep supports it, you can use -- to indicate end-of-options—it'd go before the '{}', above—though this really shouldn't be needed with find (since the found files always begin with ./)

Related Question