Grep in cPanel or Shared Hosting Environment

Being a command-line geek myself, it can be a massive pain in the ass not having SSH access to a client’s hosting service. Simple things like finding where a previous developer has declared some variable or template code can become what feels like a never-ending nightmare.

Never mind dear, PHP to the rescue…

header("Content-Type: text/plain");

// Replace $search with what you're looking for
// You could always make it a $_GET param. Yeah, that would be safe enough 😉
$search = "some string to search for";
$command = "grep -ri '$search' ./*";
$output = shell_exec($command);
echo "$output\n\n";
echo "Done.";

Keep in mind this code is basically just calling out to the command line, and that no filtering on that $search variable is performed here. Also, knowing my luck, I’ll find a host that has disabled access to shell_exec before too long.

Be careful out there 😉

Categories: Tech

‹ Previous Post Next Post ›