DirGrep.pl - Searching Text in Directory Files

This section provides a tutorial example, DirGrep.pl, to search text in files and sub directories using a recursive method to walk through a directory tree.

My next program, DirGrep.pl, is to search for lines that match the specified regular expression in all text files of the specified directory tree.

#- DirGrep.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   ($expression, $dir) = @ARGV;
   die "Missing regular expression.\n" unless $expression;
   $dir = "." unless $dir;
   $fileCount = 0;
   $matchCount = 0;
   $textCount = 0;
   $otherCount = 0;
   &loopDir($dir);
   print "Number of matched lines = $matchCount\n";
   print "Number of files with matched lines = $fileCount\n";
   print "Number of text files searched = $textCount\n";
   print "Number of other files not searched = $otherCount\n";
   exit;
sub loopDir {
   local($dir) = @_;
   local(*DIR);
   opendir(DIR, $dir) || die "Cannot open $dir\n";
   while ($f=readdir(DIR)) {
      next if ($f eq "." || $f eq "..");
      $f = "$dir\\$f";
      if (-d $f) {
         &loopDir($f);
      } elsif (-T $f) {
         $textCount++;
         if ($n=&fileGrep($f)) {
            $matchCount += $n;
            $fileCount++;
         }
      } else {
         $otherCount++;
      }
   }
   closedir(DIR);
}
sub fileGrep{
   local($file) = @_;
   open(IN, "< $file");
   $n = 0;
   $l = 0;
   while(<IN>) {
      $l++;
      if (/$expression/i) {
         $n++;
         print "$file, line $l\n" ;
         print;
         print "\n";
      }
   }
   close(IN);
   return $n;
}

Let's use the program to find files that contains the word "ActivePerl" in my Perl notes working directory tree:

>DirGrep.pl ActivePerl ..
..\htm\active_perl.html, line 3
<meta pagetitle="ActivePerl"/>

..\htm\active_perl.html, line 19
<li>How to install ActivePerl v5.6.1 on a Windows 2000 system.

..\htm\active_perl.html, line 21
<li>How to run Perl programs with ActivePerl.

..\htm\active_perl.html, line 27
Installing ActivePerl v5.6.1

......

Number of matched lines = 16
Number of files with matched lines = 3
Number of text files searched = 23
Number of other files not searched = 1

Many matches found. Now let's narrow the search to "install.*activperl". The result is much better:

>DirGrep.pl "install.*ActivePerl" ..
..\htm\active_perl.html, line 19
<li>How to install ActivePerl v5.6.1 on a Windows 2000 system.

..\htm\active_perl.html, line 27
Installing ActivePerl v5.6.1

..\htm\active_perl.html, line 40
follow the installation tool to install ActivePerl to your system

..\htm\toc.html, line 41
<li>Installing ActivePerl v5.6.1</li>

Number of matched lines = 4
Number of files with matched lines = 2
Number of text files searched = 23
Number of other files not searched = 1

Question: Can we use a variable to store the directory handle?

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

Open Directories and Read File Names

 opendir() - Open Directory to Read File Names

 opendir.pl - Sample Program to Read Directories

 DirTree.pl - Displaying the Directory Tree

DirGrep.pl - Searching Text in Directory Files

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version