How to limit grep to only finding the files you want

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

I can easily do this with a program, but it works very slowly. I want to speed up the process (so I have searched thousands of files many times), since I need to run such searches on a long list of criteria.

Usually we look for a file system using:

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

This works, but I have to use the program to exclude unwanted directories, so it works very slowly.

After exploring the topics here: flow

I decided to try several other approbations:

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

Excludes './svn', but not the './pdv' directory, does not limit the files viewed.

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

Excludes './svn', but not the './pdv' directory, does not limit the files viewed.

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

I can't even get this (or options on it) to work successfully.

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

It does not return anything. It looks like it stops as soon as it finds the .svn directory.

+4
source share
4 answers

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" {} + \) 

It will be:
- ignore the contents of directories named .svn and pdv
- grep files (and symbolic links to files) with the name *. [pwi]

The + option after exec means to collect as many files in one command as it can fit on the command line (approximately 1 million characters on Linux). This can seriously speed up processing if you need to sort through thousands of files.

+2
source

The following command finds only * .rb files containing the string require 'bundler/setup' and excludes searches in the .git and .bundle . I think this is the same use case.

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

The problem was replacing the --exclude and --exclude-dir options that I consider. Refer to the grep(1) manual.

Also note that the exclude / include parameters accept only GLOB , not regular expressions, so the suffix range of a single character can be performed with a single --include parameter, but for more complex conditions, more parameters are required:

 --include \*.[pwi] --include \*.multichar_sfx ... 
+2
source

You can try the following:

 find path_starting_point -type f | grep regex_to_filter_file_names | xargs grep regex_to_find_inside_matched_files 
0
source
 find . -name "filename_regex"|grep -v '.svn' -v '.pdv'|xargs grep -i 'your search string' 
0
source

All Articles