Perl find2perl - to find files modified in last N minutes

With the perl code I started below, I get a output of a list of file names and the check sum size
of each file in a Dir, such as:

file1,26587
file2,325874
file3,58745
file4,548787

____________________________________________________

#!/usr/bin/perl
use strict;
use CGI qw(:standard);
print qq{Content-Type: text/text\n\n};
my $fb_in    = q{/tmp/dir1};
my %Files;
 if(opendir(DIR, $fb_in)) {
    foreach my $file (readdir(DIR)) {
        if (-f qq{$fb_in/$file}) {
            $Files{$file}=1;
        }
    }
}
foreach my $key ( keys %Files ){
print STDOUT $key, ",",(unpack("%32C*",$key) % 65535) ,qq{\n};
}

____________________________________________________


what I need to do is use Perl find2perl so I can get a list as in above example but
get the list for the files that were modified in a specified time in minutes.

So what I'm trying to do is add find2perl to this script so I can run is like:

#./find_files.pl minutes=10  (or what ever value wanted)

Then it will give me the output I have above but now only for the files that were modified in
the last N of minutes specified.


Any help with adding the find2perl to this would be much appreciated.

Thanks,
bt707Asked:
Who is Participating?
 
WeiXiConnect With a Mentor Commented:
I am sorry, I forgot to type the $-sign for $ARGV[0]. The line should read as:
>   my $age_in_days = $ARGV[0]/24/60;

Calling it with
>   ./recent.pl 10
is perfectly correct.

0
 
bt707Author Commented:
The files I have in my Dir are linked to files in another Dir, I can use the find2perl command below and it will give me the files modified in less that 1 day ago.

find2perl /tmp/dir1 -mtime -1 -follow -print | perl

What I need is to find the files modified in xx minutes ago or less.

Is there a way to set the find2perl to lookup in minutes?

Thanks,
0
 
WeiXiCommented:
I don't quite see the need for find2perl. Just modify your script to test for the file age instead of file existence. -M does that in perl, age in days as for find2perl. However, changing days in minutes and vice versa is not rocket science :)

the code generated by find2perl doesn't do anything different, actually.

-M follows the symbolic links as well and gives the age of the linked file, not of the link - just to reassure you. -M also evaluates to undef if the file does not exist, so the -f test can be ommited.
   #!/usr/bin/perl
   use strict;
   use CGI qw(:standard);
   print qq{Content-Type: text/text\n\n};
-> my $age_in_days = ARGV[0]/24/60;
   my $fb_in    = q{/tmp/dir1};
   my %Files;
    if(opendir(DIR, $fb_in)) {
       foreach my $file (readdir(DIR)) {
->         if (-M qq{$fb_in/$file} < $age_in_days) {
               $Files{$file}=1;
           }
       }
   }
   foreach my $key ( keys %Files ){
   print STDOUT $key, ",",(unpack("%32C*",$key) % 65535) ,qq{\n};
   }

Open in new window

0
 
bt707Author Commented:
WeiXi,

That sounds good, but getting a error trying to run it.

I tried running it as  ./recent.pl 10

-bash-3.00$ ./recent.pl 10
syntax error at ./recent.pl line 5, near "ARGV["
Execution of ./recent.pl aborted due to compilation errors.

What am I doing wrong here, I need to get it where I can give it a xx number of minutes, example find files that were created in last 10 minutes.

Thanks,

0
 
bt707Author Commented:
WeiXi,

I should of seen that, was looking right at it but just didn't notice the missing $

Working just fine now.

Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.