#!/usr/bin/perl use strict; use warnings; use Date::Manip; use Data::Dumper; use File::Find::Rule; sub get_files { my (@dirs) = @_; my $level = shift // 2; # level to dig into my @files = File::Find::Rule->file() ->name('access.log', 'sys.log') ->maxdepth($level) ->in(@dirs); return @files; } sub searchForIP { my ($files, $ip) = @_; local @ARGV = @$files; while (<>) { print "$ARGV:$.:$_" if /$ip/; } continue { close ARGV if eof; } return; } my $numberOfDays = '2 days'; my $dateStart = ParseDate("today"); my $dateEnd = DateCalc($dateStart, $numberOfDays); # To find the every day date1 to date2 my @dates =ParseRecur("0:0:0:1:0:0:0","",$dateStart, $dateEnd); my @datesFormatted = map { UnixDate($_, '%Y-%m-%d') } @dates; # print Dumper \@datesFormatted; my @files = get_files(@datesFormatted); # print Dumper \@files; my $ip = "127.0.0.1"; searchForIP(\@files, $ip); __END__ $ perl test.pl 2018-01-03/access.log:1:127.0.0.1 This is insident 1 in 2018-01-03 2018-01-03/access.log:4:127.0.0.1 This is second insident 4 in 2018-01-03 2018-01-05/sys.log:1:127.0.0.1 This is insident 1 in 2018-01-05 2018-01-05/sys.log:4:127.0.0.1 This is second insident 4 in 2018-01-05 #### $ ls -la total 40 drwxr-xr-x 8 tinyos tinyos 4096 Jan 3 11:37 . drwxr-xr-x 5 tinyos tinyos 4096 Jan 2 20:38 .. drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 10:01 2018-01-01 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 10:02 2018-01-02 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 11:33 2018-01-03 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 10:02 2018-01-04 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 11:34 2018-01-05 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 11:27 2018-01-06 -rw-r--r-- 1 tinyos tinyos 1230 Jan 3 11:37 test.pl -rw-r--r-- 1 tinyos tinyos 414 Jan 3 10:26 test.pl~ #### $ ls -la 2018-01-01/ total 8 drwxr-xr-x 2 tinyos tinyos 4096 Jan 3 10:01 . drwxr-xr-x 8 tinyos tinyos 4096 Jan 3 11:37 .. -rw-r--r-- 1 tinyos tinyos 0 Jan 3 10:01 access.log -rw-r--r-- 1 tinyos tinyos 0 Jan 3 10:01 sys.log #### $ cat 2018-01-03/access.log 127.0.0.1 This is insident 1 in 2018-01-03 127.0.0.2 This is insident 2 in 2018-01-03 127.0.0.3 This is insident 3 in 2018-01-03 127.0.0.1 This is second insident 4 in 2018-01-03