#!/usr/bin/perl use strict; use IO::All; use warnings; use Data::Dumper; use File::Find::Rule; my @LogDirs = ('/home/user/PerlMonks/', '/tmp/Monks'); # add more here my $level = shift // 3; # level to dig into my @files = File::Find::Rule->file() ->name( '*.txt', '*.log' ) #can insert regex too ->maxdepth($level) ->in(@LogDirs); my %hash; foreach my $file (@files) { my @lines = io($file)->chomp->slurp; my $matches = grep {/Start/ or /middle/ or /end/} @lines; $hash{$file} = $matches; } print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { '/home/user/PerlMonks/Foo/Bar/monks.log' => 1, '/home/user/PerlMonks/sample.txt' => 1, '/tmp/Monks/SampleDir/monks.log' => 2 }; #### $ cat /tmp/Monks/SampleDir/monks.log Start line key line. Ignore this line. Another line key end. $ cat /home/user/PerlMonks/Foo/Bar/monks.log This is line 1. This key line end. $ cat /home/user/PerlMonks/sample.txt This is another key line middle point. I do not care about this.