Thursday, June 4, 2009

How to grep in files by specific extension

For example lets search for "*.php" files

We should have something like this:

find ./ -name "*.php"

Now let grep for specific string "$GLOBALS" in a specific php file - test.php

grep -n "$GLOBALS" test.php

Now let combine what we learn

find ./ -name "*.php" -type f -exec grep -H -n "$GLOBALS" {} \;

the above code search the current directory for php files and search in them "$GLOBALS"

-H -n means show me filename and line number.

This should work.

For best results use GUI editor with search in files :-)

For more info

man grep

0 comments: