Jul
13
2009

Search in Files using PHP Script with unix commands

Search 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}

Related Posts

About the Author: Jignesh Patel

Jignesh Patel is a geek, an engineer & a web developer, who focus on developing web sites which look great, work great and can still work on older web browsers. Expertise in PHP, MySQL, JS, Magento & Wordpress.

  • http://www.ferommok.com/ Dirnov

    Hi, Everything dynamic and very positively! :)
    Thanks
    Dirnov

  • http://allzoom.ru.ru Zashkaser

    If you are wondering how you can help with this or future events, please contact us . Also, you can contact other

  • http://allzoom.ru Sdanektir

    Excellent site. It was pleasant to me.

  • http://vmuz.ru Vivalkakira

    i found you by link from the Directory Listing Script from Ash.. Nice to read your blog ^.^

  • http://jignesht.blogspot.com Jignesh Thummar

    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

    Hi Jignesh,
    Thanks for your comments. I will surely implement your suggestions.

blog comments powered by Disqus