• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1332
  • Last Modified:

grep directory exclusion

I want to be able to find matched text in only php files with grep and exclude it from looking in certain directories, or tell it a list of directories to only look in.

If it recurses down into one of my directories that contains 100,000+ files it just takes forever to be useful.

Right now I can only do this by running grep 3 times like:
grep "textToMatch" *.php
grep "textToMatch" inc/*.php
grep "textToMatch" lib/*.php

--include and --exclude seem to be for files and not directories, it will still recurse into my large directory and take hours to complete.

Is there one command to do it it all?  I'm running linux fedora 5.
Thanks.
0
burnsj2
Asked:
burnsj2
  • 3
2 Solutions
 
amit_gCommented:
Would this work for you?

grep "textToMatch" *.php inc/*.php lib/*.php
0
 
burnsj2Author Commented:
it does, but is there a way to just exclude the directory "images"? because my list of directories to only include is quite long.
0
 
TintinCommented:
Is your list of include directories longer than your exclude list?
0
 
amit_gCommented:
For that you will have to use find. Try something like this...

find `find . -type d | grep -iv image` -maxdepth 1 -name '*.php' -exec grep "textToMatch" '{}' \;
0
 
amit_gCommented:
You will probably want to see the filenames also in the grep matches so use

find `find . -type d | grep -iv image` -maxdepth 1 -name '*.php' -exec grep -H "textToMatch" '{}' \;
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now