Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

right need a script/command that will list all .php files that have a pattern/string in it.

It should look at the current directory and all sub directories.

Be even better if it showed the line number something like:

my new command 
./www/index.php Line 12
./www/lib/config.php Line 123

Also would it be possible to do a search and replace for the line in each file that has this pattern?

share|improve this question

6 Answers 6

up vote 5 down vote accepted

grep can do this on it's own:

grep -rn --include='*.php' "pattern" .
share|improve this answer
    
this lists the file name and the text in the file is there a way to exclude the text in the file in the list.. as in i just want a list of files? –  Derek Organ Nov 25 '09 at 16:53
1  
You can use -l, but it will suppress the line number too. It can however be retained using cut: grep -rn --include='*.php' "pattern" . | cut -d: -f1-2 –  Mikael S Nov 25 '09 at 17:47
    
thanks.. worked a charm. –  Derek Organ Nov 25 '09 at 17:51

find . -name '*.php' -print0 | xargs -0 grep -n "pattern"

share|improve this answer
    
yea this lists the file and the line in the file. very useful but is there a way to replace the line with something if found? –  Derek Organ Nov 25 '09 at 12:52

I think the following will do the search and replace, but make sure you test this first because I haven't:-)

find . -type f -name '*.php' -print0 | xargs -0 sed -i '.bak' -e 's/foo/bar/g'

This will back up each file before it edits it by create filename.php.bak files, and it is going to update time stamps on all the files. It replaces 'foo' with 'bar'. This also should be safe for filenames with spaces in it.

share|improve this answer

find . -name '*.php' |xargs -I{} perl -i.bak -pe 's/pattern/replacement/' {}

This does a find of every php file. Xargs then executes the command for each input on STDIN passing the value as {}. Then Perl reads in the file, renames it with the .bak extension, and executes the Perl expression on every line in the file.

share|improve this answer

Will this not do to replace the pattern?

find . -name "*.php" -exec sed -i".bak" 's/pattern/replace/g' {} \;

Very dangerous though! Make sure you keep the pattern unique. :-)

share|improve this answer

Use ack, better than grep.

Advantages (for more see the Top 10 reasons to use ack instead of grep):

  • Searches recursively by default
  • Can recognize sets of common files (--php) to search only php files for example
  • Supports Perl-style regexs since it is actually pure Perl.
  • Colored by default

For replacement operations, you might use ack in combination with perl or sed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.