LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2010, 02:42 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Rep: Reputation: 67
recursively searching PHP or HTML files


I'm a frequent user of grep. I know that I can recursively search a directory using the -r flag:

Code:
// will recursively search all files
grep -r 'some string' *
However, if I want to limit my search to PHP files, the -r flag is suddenly useless:
Code:
// for some reason, this only searches the PHP files in the current dir
grep -r 'some string' *.php
Can anyone tell me a good way to recursively search a directory and its subdirs for a string but ONLY look at PHP or HTML files (and possibly TXT files too) ? I'm really hoping for a nice, short command that doesn't involve using an exclude file and which isn't really painful to type. I do this kind of search very frequently and have resorted to either searching EVERY file which is really slow (TAR and ZIP files really slow it down) OR typing repeated commands to search *.php, */*.php, etc.
 
Old 05-05-2010, 03:41 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 548

Rep: Reputation: 116Reputation: 116
Use the find command either with exec or piping to a while loop and grep'ing

Code:
find . -name "*.php" -exec grep somestring '{}' \;
Code:
find . -name "*.php" | while read i
do
  grep somestring $i
done

Last edited by devnull10; 05-05-2010 at 03:48 PM.
 
Old 05-05-2010, 04:03 PM   #3
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Thanks for your response.

I had considered the find command. I've been using it a bit and only recently discovered the -exec flag. What's with the weird bracket syntax ?? This seems to work:
Code:
find * -name '*.php' -exec grep -l 'some string' {} \;
I think I understand the brackets...that's where you want to iterate the find results, right? What's with the semicolon and backslash?

Grep also allows you to specify one type of file with the --include flag. I still don't understand why specifying the file as *.php disables the recursive grep.
Code:
grep -rl --include=*.php 'some string' *
These two different searches appear to return the same results.

I was kind of hoping for some compact way to search multiple file types (like .html, htm, .php, .php4, etc.). Any thoughts about that would be much appreciated. I was hoping I could create some kind of include file (e.g., ~/ginc.txt) in which I could specify any file types I'd like to commonly search, but grep does not implement an --exclude-from flag like tar does. Any tips about how to make this search more convenient would be much appreciated.
 
Old 05-05-2010, 10:07 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Here's a shell prog that you just feed the params to
Code:
# Extract the search string (param 1) and remove it from param list
string=$1
shift

# for each file extension (ie remaining param list)
for ext in $*
do
    # Recursive search for files with current extension
    for file in `find . -name "*.$ext"`
    do
        grep $string $file
        if [[ $? -eq 0 ]]
        then
            # matched, so say filename inc dir path
            echo $file
        fi
    done
done
./myfind.sh <string to find> <extension1> <extension2> ....
 
Old 05-05-2010, 10:32 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
{} - what was found using your find arguments

\; - -exec requires a termination of command and a simple semicolon ( if not escaped appears as part of your command separating to the next one, eg. -exec grep blah {}; ls blah \;

Quote:
search multiple file types (like .html, htm, .php, .php4, etc.)
Depends on how long this grows and if it is just variations as you have shown:
For your variations:
Code:
 find . -name '*.htm*' -o -name '*.php*'
 
Old 05-06-2010, 01:30 PM   #6
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Excellent info, guys. Thanking...
 
Old 05-06-2010, 09:59 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
No probs. If you have a solution, please mark as SOLVED.
 
  


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
How to remove script injection from .php and .html files spithakos Linux - Security 14 09-22-2011 04:11 PM
Where to save the php and html files on apache Zepyr Linux - Newbie 5 07-01-2009 04:16 AM
gentoo - php in html files? Izarf Linux - Server 1 05-28-2009 11:10 AM
Searching for a PATTERN in source files RECURSIVELY dsuratman Linux - General 6 11-23-2007 02:51 AM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration