How to limit grep to only search the files that you want

We have a rather large and complex file system and I am trying to generate a list of files containing a particular text string. This should be simple, but I need to exclude the './svn' and './pdv' directories (and probably others) and to only look at files of type *.p, *.w or .i.

I can easily do this with a program, but it is proving very slow to run. I want to speed up the process (so that I'm not searching thousands of files repeatedly) as I need to run such searches against a long list of criteria.

Normally, we search the file system using:

find . -name "*.[!r]*" -exec grep -i -l "search for me" {} ;

This is working, but I'm then having to use a program to exclude the unwanted directories , so it is running very slowly.

After looking at the topics here: Stack Overflow thread

I've decided to try a few other aproaches:

grep -ilR "search for me" . --exclude ".svn" --excluse "pdv" --exclude "!.{p,w,i*}" 

Excludes the './svn', but not the './pdv' directories, Doesn't limit the files looked at.

grep -ilR "search for me" . --exclude ".svn" --excluse "pdv" --include "*.p" 

Excludes the './svn', but not the './pdv' directories, Doesn't limit the files looked at.

find . -name "*.[!r]*" -exec grep -i -l ".svn" | grep -i -l "search for me" {} ;

I can't even get this (or variations on it) to run successfully.

find . ! -name "*.svn*" -prune -print -exec grep -i -l "search for me" {} ;

Doesn't return anything. It looks like it stops as soon as it finds the .svn directory.


How about something like:

find . ( ( -name .svn -o -name pdv ) -type d -prune ) -o ( -name '*.[pwi]' -type f -exec grep -i -l "search for me" {} + )

This will:
- ignore the contents of directories named .svn and pdv
- grep files (and symlinks to files) named *.[pwi]

The + option after exec means gather as many files into a single command as will fit on the command line (roughly 1 million chars in Linux). This can seriously speed up processing if you have to iterate over thousands of files.


Following command finds only *.rb files containing require 'bundler/setup' line and excludes search in .git and .bundle directories. That is the same use case I think.

grep -ril --exclude-dir .git --exclude-dir .bundle 
  --include *.rb "^require 'bundler/setup'$" .

The problem was with swapping of --exclude and --exclude-dir parameters I believe. Refer to the grep(1) manual.

Also note that exclude/include parameters accept GLOB only, not regexps, therefore single character suffix range can be done with one --include parameter, but more complex conditions would require more of the parameters:

--include *.[pwi] --include *.multichar_sfx ...

您可以尝试以下方法:

find path_starting_point -type f | grep regex_to_filter_file_names | xargs grep regex_to_find_inside_matched_files
链接地址: http://www.djcxy.com/p/47248.html

上一篇: 为GREP在bashrc中全局化文件类型

下一篇: 如何限制grep只搜索你想要的文件