Search script

From Wiki

Jump to: navigation, search

Search for a String (piece of text) in a Directory


#Folder to Begin Search #Mine First $phpdir = "/home/steviewdr/"; #$phpdir = "/home/";
#Log File $logfile = "/home/steviewdr/mysql_php.log";
#What to Search for $search = "localhost"; #What to Replace if Required $replace = "";
# Find all the php files we're looking for $findcommand = "find $phpdir -name \"*.php\" -exec grep -q $search {} \\; -print"; print $findcommand; $findphpfiles = `$findcommand`; print "Here's what it found:\n"; print $findphpfiles; print "\n";
@phpfiles = split(/\n/, $findphpfiles); open (LOG, "> $logfile") or die "Can't open $logfile: $!\n"; print "Checking each file......\n";
# go through each file and see if it says localhost in there # if so, we may need to edit and check it $ans = "n"; foreach $file (@phpfiles) { print "$file\n"; # read in all the lines open (PHPFILE, $file) or die "Can't open $file: $!\n"; @phplines = <PHPFILE>; close PHPFILE;
$lineno = 1; foreach $line (@phplines) { #$found = 0;
if ($line =~ m/$search/) { #$newline =~ s/$othersearch/$replace/g; print LOG "$file:$lineno\n\t$line"; #$found = 1; } $lineno = $lineno + 1; } close PHPFILE; } close (LOG);
Personal tools