Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I search all the text files in a tree (but not the binaries) for a certain string

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.

like image 580
John Lawrence Aspden Avatar asked Oct 18 '11 15:10

John Lawrence Aspden


1 Answers

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.

like image 136
evil otto Avatar answered Sep 18 '22 18:09

evil otto