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?
grep
can do this on its own:find . -name '*.php' -print0 | xargs -0 grep -n "pattern"
I think the following will do the search and replace, but make sure you test this first because I haven't:-)
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.
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.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. :-)
Use ack, better than grep.
Advantages (for more see the Top 10 reasons to use ack instead of grep):
For replacement operations, you might use ack in combination with perl or sed.