Solved

grep directory exclusion

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to rebuild XFS volume from LV 19 90
android secure ftp 3 52
Disabling security updates Ubuntu 3 31
WordPress: Debugging from my Windows 10 Desktop 6 38
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…
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…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

831 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