Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

grep directory exclusion

Posted on 2007-03-20
5
Medium Priority
?
1,330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

618 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