LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: get file date (https://www.linuxquestions.org/questions/programming-9/perl-get-file-date-378881/)

kenneho 11-01-2005 05:19 AM

Perl: get file date
 
How do I iterate the files in a directoy and add the files to a list based on the date it was last modified?

tlowk 11-01-2005 06:12 AM

Are you looking for something like this?


#!/usr/bin/perl -w

sub datecompare {
my ($a,$b)=@_;
my @astat=stat($a);
my @bstat=stat($b);
return ($astat[10])<=>($bstat[10]);
}
opendir($dh,".");
@lines=readdir($dh);
closedir($dh);

print join("\n",sort datecompare @lines)

kenneho 11-01-2005 09:24 AM

Thanks for the reply.


I'm terrible sorry - having re-read my question in the first post, I see that it is not correct. What I really ment is this:



I want my script to add to a list only the files (i.e., filenames) modified since last time the script was run. I'm thinking I should have a file containing only the time the script was last run, and compare this with the other files in the direcetory. To be a little more precise: I'm developing a program, and I want to sort out those files having been modified since the last time the script was run. This script copies the modified source files to machines on the network, and I want to send only the source files having been modified (or else the machines will waste a lot of time re-compiling files that have not been modified). Any ideas?

kenneho 11-01-2005 09:44 AM

I think I figured it out after doing some :study:

tlowk 11-01-2005 09:53 AM

I think you'd better check for rsync instead writing a script for this

but this might be a way to do it

#!/usr/bin/perl -w

use strict;
my $dh=undef;
my $checkfile="reffile";
opendir($dh,".");
my @lines=readdir($dh);
closedir($dh);

my @newlist=();
my @refstat=stat($checkfile);
my @fstate;
for my $file (@lines) {
@fstate=stat($file);
if ($fstate[9] > $refstat[9]) {
push @newlist,$file;
}
}
`touch "$checkfile"`

kenneho 11-01-2005 11:16 AM

Haven't heard of rsync before you mentioned it, but after a quick glance at the man pages it seems to be a better solution. I'll check both rsync and your script to see which of these is most suitable for my script. Thanks a lot!


All times are GMT -5. The time now is 05:49 PM.