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

My best shot so far (for finding 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 very well, but it relies on all the interesting things found in the .mk makefiles, .c or .h source files and .s assembly files.

I was thinking of adding things like “all files called Makefile” or “all * .py python scripts”, but the question is, what would be easier if you could find find, to find only text files.

If you just run grep for all files, it takes a long time, and you get a lot of uninteresting calls to object files.

+6
source share
4 answers
 grep -rI <path> <pattern> 

The '-r' switch performs the grep recurse function, and the '-I' ignores binaries.

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

+6
source

GNU grep supports the -I option, which allows binary files to be processed (as defined when viewing the first few bytes) as if they did not match, so they are essentially skipped.

+11
source

Did you watch ack ?

Of the top 10 reasons to use ack:

ack ignores most of the crap you don't want to look for

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

You can use grep -I to ignore binary files. Using GNU Parallel instead of xargs will allow you to split your work into multiple processes, using multiple parallelism to speed things up.

There is an example of how to execute 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" 
+2
source

Source: https://habr.com/ru/post/899536/


All Articles