Solved

Perl find2perl - to find files modified in last N minutes

Posted on 2009-04-02
5
1,189 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 105
PERL export multiple query results to a JSON file 1 173
rename outfile before writing 2 74
Exchange 2010 Transport Rule Regex 28 123
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

821 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