search text in files

by Guna 2012-10-10 16:08:47

Think of a scenario where you need to remove a piece of code in your files that has a particular word in it. This can be easily done in linux terminal by issuing following command,

find . | xargs grep 'getTags' -sl

Above command finds the files which has 'getTags' in it, similar to the results given below:


Now it becomes easy to remove the code in those files only instead of searching all the files. But when your files hosted in shared server you wont get terminal access, what will you do?

You need to bring the same functionality through php code. So create a file called "find.php" and paste the following code in it.


passthru("find . | xargs grep '$q' -s ");
echo "<h3>Search results for "$q"</h3>";
echo "<pre>";print_r($matches);echo "</pre>";

then access the file like this,


This is for our internal use only and we need to protect it from being indexed in search engines, so add following code in htaccess:

<Files "find.php">
AuthName "Member Only"
AuthType Basic
AuthUserFile /home/username/.htpasswd
require valid-user

Before placing this code you need to create a htpasswd file with a username and password

Tagged in:


You must LOGIN to add comments