Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 10-11-2005, 12:30 AM   #1
Senior Member
Registered: Dec 2004
Posts: 1,055

Rep: Reputation: 78
can you specify which files to grep search?

i have a complex directory with many files and many subdirectories. I would like to grep for patterns in some php files, html files, javascript files, while ignoring .gz files, image files, and files in certain directories.

is there a way to do this? i have tried the --include and --exclude flags for grep but they don't appear a) to support multiple file extensions in a single expression b) to allow you to exclude entire subdirectories.

I'm guessing that there might be some way to create a list of the files i want searched and perhaps pipe that to the grep command? that would be awesome. if someone could help me, i would appreciate it.
Old 10-11-2005, 01:02 AM   #2
Senior Member
Registered: Jun 2004
Distribution: Arch, Debian, Slack
Posts: 1,016

Rep: Reputation: 47

grep -i '<string>' | ./*.php *.html *.js

that would search only php, html, and js files while ignoring anything else in the directory.
Old 10-11-2005, 03:17 AM   #3
Senior Member
Registered: Dec 2004
Posts: 1,055

Original Poster
Rep: Reputation: 78
thanks for the suggestion...i need it to recurse subdirs (excepting ones i want to specifically exclude).

the ideal situation would be to generate a file list and only check those files..
Old 10-11-2005, 09:11 AM   #4
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
I think this can do what you ask :

find . -type f \( -name \*.php -o -name \*.html -o -name \*.js \) | grep -v -e <dir1_to_exclude> -e <dir2_to_exclude> | xargs grep --with-filename <string_to_search>

Hope this helps.

Last edited by berbae; 10-11-2005 at 09:14 AM.
Old 10-12-2005, 09:28 PM   #5
Senior Member
Registered: Dec 2004
Posts: 1,055

Original Poster
Rep: Reputation: 78

pretty gnarly. i will give that a try.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
To recursively search with grep grautu Slackware 5 11-21-2005 03:53 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
grep search in zip/rar... etc plainkeyman Linux - Software 2 05-23-2005 07:05 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 04:29 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 10:01 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration