Andrew Libby on 22 Apr 2006 00:14:08 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Perl question


Hi Eric,

Here's a quick one that will hopefully give you enough of what you
need to carry out what you're up to.

Not sure how perl savy you are, so it may be a bit over (or under)
annotated:

#!/usr/bin/perl -w

use strict;
use File::Find;

sub ctime {
    my $f = shift;
    # Stat returns a list of file attributes.  Do perldoc -f stat for
details.  ctime is the 11th
    # item in the list.
    ( stat $f )[10];
}

my $f1 = shift;
my $f2 = shift;

if( ctime($f1) > ctime($f2) ) {
    print "$f1 newer than $f2\n";
} else {
    print "$f1 older than $f2\n";
}

# Here we collect a bunch of file status information in a list and
# then sort it by ctime (again 11th item in the stat array).

my @l;

find sub {
    push @l, {
        file => $File::Find::name,
        info => [ stat( $File::Find::name ) ]
    };
}, '/etc/sysconfig/';

@l = sort { $a->{'info'}->[10] <=> $b->{'info'}->[10] } @l;

for (@l) {
    print $_->{'file'} . " " . localtime($_->{'info'}->[10]) . "\n";
}


Good luck with it.

Andy


Eric wrote:

>In the shell I can easily compare the dates of two files:
>
>$  if [ .y2log -nt .y2log-1 ]; then
>     echo .y2log is newer than .ylog-1
>   else
>     echo .y2log is older than .ylog-1
>   fi
>.y2log is newer than .ylog-1
>$
>
>So what is the similar perl idiom for comparing file dates?
>
>I have to compare 166,000 (+) pairs of files and I'd rather
>do it with perl.  Particularly since I open the directory
>and read the filenames into a hash which turns out to be
>wicked-fast for processing the file names.
>
>Thanks,
>
>Eric
>
>  
>


-- 
Andrew Libby                                  
alibby@philadelphiariders.com
http://philadelphiariders.com/


___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug