Skip to content

Instantly share code, notes, and snippets.

@icheernoom
Created February 3, 2015 14:44
Show Gist options
  • Save icheernoom/c794b4901771c377e676 to your computer and use it in GitHub Desktop.
Save icheernoom/c794b4901771c377e676 to your computer and use it in GitHub Desktop.
Search String in Files on Directory and Subdirectory
use File::Find;
print "\n\n\t### Search String in Files on Directory and Subdirectory By ICheer_No0M ###\n\n";
print 'Path File : ';
chomp($path=<stdin>);
print 'Search Keyword : ';
chomp($string=<stdin>);
$keyword = AddSlashes($string);
find(\&file_names, $path);
sub file_names {
if($_ =~ /\.php$/){
$file = $File::Find::name;
open (MYFILE, $file);
$line = 1;
while (<MYFILE>) {
$content .= $_;
if($content=~/$keyword/){
print "\nFound : $string\n";
print "On Line : $line\n";
print "On File : $file\n";
}
$line += 1;
$content = "";
}
close (MYFILE);
}
}
print "\n\n\t### Finding $string on $path Complete... ###\n\n";
sub AddSlashes {
$text = $_[0];
$text =~ s/\\/\\\\/g;
$text =~ s/\$/\\\$/g;
$text =~ s/\'/\\'/g;
$text =~ s/\"/\\\"/g;
$text =~ s/\[/\\[/g;
$text =~ s/\]/\\\]/g;
$text =~ s/\(/\\(/g;
$text =~ s/\)/\\\)/g;
$text =~ s/\\0/\\\\0/g;
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment