Saturday, July 5, 2014

When Grep FAILS, Find works

Couple of times over the linux and mac platform I found the grep failing miserably when trying to search for  a word in a present directory like

grep -ri "context" .  {search for all files having context word appear anywhere in the file. i for ignore case, r is recursively}

find came to the rescue. find is also recommended because of the speed and ability to deal with files having spaces in their names

Couple of commands the worked real well for me on Mac and Linux platforms :

find ~/workspace/directoryspace/fileswithsimplenames/ -type f -exec grep -ri "Context" {} +


In case you want find to deal with spaces and other metacharacters, use the print()

find ~/workspace/directoryspace/fileswithawefulnames/ -type f -print0 | xargs -0 grep -l "foo"