Solved

grep directory exclusion

Posted on 2007-03-20
5
1,313 Views
Last Modified: 2013-12-06
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
Comment
Question by:burnsj2
  • 3
5 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 18760603
Would this work for you?

grep "textToMatch" *.php inc/*.php lib/*.php
0
 
LVL 1

Author Comment

by:burnsj2
ID: 18760638
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
 
LVL 48

Expert Comment

by:Tintin
ID: 18760662
Is your list of include directories longer than your exclude list?
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 500 total points
ID: 18760689
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
 
LVL 58

Expert Comment

by:amit_g
ID: 18760701
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now