Solved

Perl find2perl - to find files modified in last N minutes

Posted on 2009-04-02
5
1,184 Views
Last Modified: 2012-05-06
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,
0
Comment
Question by:bt707
  • 3
  • 2
5 Comments
 

Author Comment

by:bt707
Comment Utility
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
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
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
 

Author Comment

by:bt707
Comment Utility
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
 
LVL 3

Accepted Solution

by:
WeiXi earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:bt707
Comment Utility
WeiXi,

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

Working just fine now.

Thanks!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now