User Tools

Site Tools


search_script

Search_script

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

#!/usr/bin/perl <br> #Folder to Begin Search #Mine First $phpdir = “/home/steviewdr/”; #$phpdir = “/home/”; <br> #Log File $logfile = “/home/steviewdr/mysql_php.log”; <br> #What to Search for $search = “localhost”; #What to Replace if Required $replace = “mysql.server.ie”; <br> # 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”; <br> @phpfiles = split(/\n/, $findphpfiles); open (LOG, “> $logfile”) or die “Can't open $logfile: $!\n”; print “Checking each file……\n”; <br> # 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; <br> $lineno = 1; foreach $line (@phplines) { #$found = 0; <br> 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);

search_script.txt · Last modified: 2022/07/19 20:13 by 127.0.0.1