6

My best shot so far is (for looking for strings in a directory containing a large C program)

find ~/example_directory -type f \( -name "*.mk" -or -name "*.[sch]" \) -print0 | xargs -0 -e grep "example_string"

Which works pretty well, but it relies on all the interesting things being in .mk makefiles, .c or .h source files, and .s assembler files.

I was thinking of adding in things like 'all files called Makefile' or 'all *.py python scripts', but it occurs that it would be way easier if there were some way to tell find only to find the text files.

If you just run grep on all files, it takes ages, and you get lots of uninteresting hits on object files.

6
grep -rI <path> <pattern>

The '-r' switch makes grep recurse, and '-I' makes it ignore binary files.

There are additional switches to exclude certain files and directories (I frequently do this to exclude svn metadata, for example)

11

GNU grep supports the -I option, which makes it treat binary files (as determined by looking at the first few bytes) as if they don't match, so they are essentially skipped.

1
2

Have you looked at ack?

From the top 10 reasons to use ack:

ack ignores most of the crap you do not want to search

  • ...
  • binary files, core dumps, etc
2

You can use grep -I to ignore binary files. Using GNU Parallel instead of xargs will allow you to break up the work into multiple processes, exploiting some parallelism for speedup.

There is an example of how to perform a parallel grep available in the documentation: http://www.gnu.org/s/parallel/man.html#example__parallel_grep

find -type f | parallel -k -j150% -n 1000 -m grep -I "example_string"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.