In: PHP
13 Jul 2009Search in large number files is painful & slow for the Magento based projects.
If you are using your editor’s built-in search feature, then also it may take lots of time to search within all directories. In case of you are editing the files on the live server, its very awful to search in even small no. of files for code.
However, there is a solution, if your Magento store is hosted on Linux server. You can get advantages of unix commands.
I made a PHP script which can search in the directory. You can set the directory path in which you want to perform the search.
The example script below can search in ‘app’ directory of your Magento setup. Put the script in the root directory.
<?php
ini_set('max_execution_time', 3600);
ini_set("memory_limit","256M");
$item = $_GET['item'];
if(trim($item) != '')
{
$script = "grep -n -r '$item' ".dirname(__FILE__)."/app";
// replace 'exe c' with 'exec' in this line
$results = exe c($script,$retval);
if($results) {
echo "Search Done";
foreach ($retval as $value) {
echo "".$value;
}
}
else {
echo "Search Done, No results found.\n";
}
}
else {
echo "No Search performed";
}
?>
Name it search.php, then you will need to execute the script from browser this way: http(s)://{host}/search.php?item={keyword}
Hello, Welcome to the Magento Coder.
I am Jignesh Patel, a Web Developer.
About Jignesh
6 Responses to Search in Files using PHP Script with unix commands
Dirnov
July 18th, 2009 at 5:46 pm
Hi, Everything dynamic and very positively!
Thanks
Dirnov
Zashkaser
August 6th, 2009 at 8:39 am
If you are wondering how you can help with this or future events, please contact us . Also, you can contact other
Sdanektir
August 7th, 2009 at 4:18 am
Excellent site. It was pleasant to me.
Vivalkakira
August 7th, 2009 at 11:46 pm
i found you by link from the Directory Listing Script from Ash.. Nice to read your blog ^.^
Jignesh Thummar
September 10th, 2009 at 1:02 am
Hi, good one. But i have a suggestion for you. If you are searching into file, you might be interested into line number of the search keyword.
for that i would recommend you to use following command:
$script = “grep -n -r ‘$item’ “.dirname(__FILE__).”/app”;
If you are using this command you might need to remove htmlentities(), to make it proper display. Though not sure on it.
I think, ‘grep’ command is faster then using lots of ‘piped-find’ command. Another advantage of grep is that you can apply pattern(this is where command line power comes in picture) inside, but for that you have to validate patterns in your script. For example, if you want to search either “this” or “url” using grep, you have to pass it as “this\|url”
-Jignesh
admin
September 10th, 2009 at 9:43 am
Hi Jignesh,
Thanks for your comments. I will surely implement your suggestions.