Solved

grep directory exclusion

Posted on 2007-03-20
5
1,317 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

786 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