LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-10-2005, 11:30 PM   #1
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Rep: Reputation: 49
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, 12:02 AM   #2
slackhack
Senior Member
 
Registered: Jun 2004
Distribution: Arch, Debian, Slack
Posts: 1,016

Rep: Reputation: 46
try:

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, 02:17 AM   #3
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Original Poster
Rep: Reputation: 49
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, 08:11 AM   #4
berbae
Member
 
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 08:14 AM.
 
Old 10-12-2005, 08:28 PM   #5
sneakyimp
Member
 
Registered: Dec 2004
Posts: 791

Original Poster
Rep: Reputation: 49
wow!

pretty gnarly. i will give that a try.
THANKS!
 
  


Reply


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 02:53 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM
grep search in zip/rar... etc plainkeyman Linux - Software 2 05-23-2005 06:05 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 03:29 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM


All times are GMT -5. The time now is 06:15 AM.

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