Scripting >> Perl >> How to scan a directory and search for files containing a specific string

following example scans the /apache/htdocs directory and searches each file for the pattern "<A HREF=". Only matching files will be displayed. #!perl # example $htmlroot = "/apache/htdocs"; $dirname = $htmlroot; #$dirname = "..\/htdocs"; #chdir("/apache/cgi-bin/"); opendir(DIR,$dirname); @files = grep{!/^\./} readdir(DIR);# remove the dots closedir(DIR); chdir("$dirname"); foreach $file (@files) { @fileinfo = stat($file); $modify_time = localtime($fileinfo[9]); $pattern="<A HREF="; open(inhandle, "$file"); while (<inhandle>) { if ( (/$pattern/gi) ) { chomp; print "[$dirname\/$file] $_\n"; print "----------------------------------------\n"; } } close(inhandle); }