http://www.perlmonks.org?node_id=89501

Recursive Search and Grep Utility Written by sierrathedog04, sierrathedog04@hotmail.com, 6/01

  • The first parameter is a regular expression which matches the filenames that you wish to search for. It defaults to '.*'
  • The second parameter is a regular expression for which you wish to grep your found files. It defaults to '.*'
  • The third parameter is the directory from which you will start your recursive search. It defaults to '.'
Tested on Perl 5.005_02 in a MP-RAS Unix environment.
Updated 6/19 to add line inadvertently left off original posting.
use strict; use File::Find; my $glob = shift || '.*'; my $grepTerm = shift || '.*'; my $dir = shift || "."; #following line added 6/19. (Inadvertently left off original posting:) find (\&Wanted, $dir); sub Wanted { return unless /$glob/; open (CURRENT_FILE, $_); my @currentFile = <CURRENT_FILE>; my @foundLines = grep /$grepTerm/, @currentFile; print "In $File::Find::dir/$_:\n" if @foundLines; foreach (@foundLines) { print " $_\n"; } } print "Completed recursive search for files named $glob containing ter +m $grepTerm in directory $dir";