Solved

Perl find2perl - to find files modified in last N minutes

Posted on 2009-04-02
5
1,192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:bt707
ID: 24055395
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
ID: 24055542
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
ID: 24055617
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
ID: 24055768
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
ID: 31566068
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

728 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