LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2005, 05:19 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Rep: Reputation: 40
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?
 
Old 11-01-2005, 06:12 AM   #2
tlowk
Member
 
Registered: Nov 2003
Location: Belgium
Distribution: Slackware
Posts: 151

Rep: Reputation: 23
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)
 
Old 11-01-2005, 09:24 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
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?
 
Old 11-01-2005, 09:44 AM   #4
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
I think I figured it out after doing some
 
Old 11-01-2005, 09:53 AM   #5
tlowk
Member
 
Registered: Nov 2003
Location: Belgium
Distribution: Slackware
Posts: 151

Rep: Reputation: 23
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"`
 
Old 11-01-2005, 11:16 AM   #6
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl/bash - calculate date in future rose_bud4201 Programming 6 05-10-2009 05:59 AM
get date (file) os2 Programming 1 03-07-2005 11:58 AM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
perl and date manipulation Syncrm Programming 5 10-28-2002 02:05 AM


All times are GMT -5. The time now is 09:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration