#!/usr/bin/perl ########################################### # findgrep -- Find files with content # matching a pattern # Mike Schilli, 2004 (m@perlmeister.com) ########################################### use strict; use warnings; use File::Find; use Getopt::Std; use strict; our $CVSVERSION = '$Revision: 1.2 $'; getopts('i', \my %options); my ($startdir, $pattern) = @ARGV; (defined $startdir && defined $pattern) || usage(); (-d $startdir) || die "Cannot open directory $startdir"; $pattern = "(?i)$pattern" if $options{i}; File::Find::find(\&fc, $startdir); ###################################################################### sub fc { my $file = $_; return unless -f $file; return if -s > 1_000_000; return unless -T _; open(FILE, "<$file") || warn "Cannot open $file"; while() { if(/$pattern/o) { print "$File::Find::dir/$file: $_"; } } close(FILE); $_ = $file; } ###################################################################### sub usage { $0 =~ s#.*/##g; print "usage: $0 startdir pattern\n"; exit 0; } __END__ =head1 NAME findgrep -- Find files with content matching a pattern =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS findgrep [-i] startdir pattern =head1 DESCRIPTION C recursively searches directories for text files containing specified patterns. If the C<-i> option is given, the search is case-insensitive. =head1 EXAMPLES findgrep /etc/rc.d mysql =head1 LEGALESE Copyright 2004 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2004, Mike Schilli