Solved

grep directory exclusion

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
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…

912 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

21 Experts available now in Live!

Get 1:1 Help Now