Grep in cPanel or Shared Hosting Environment

June 28th, 2018
159 words · 1 minutes read · 0 Comments

Being a command-line geek myself, it can be a massive pain in the ass not having SSH access to a clients 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…

1
2
3
4
5
6
7
8
9
10
<?php
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 😉


Posted in: Tech
Tagged: PHP, cPanel, server

Leave a comment