Code Comments
Programming Forum and web based access to our favorite programming groups.Hi,
I have written a small script that is supposed to tell me the oldest file in
my directory (as per ctime). I have read the various times the various file
s were created, into an array called times. I have then sorted this array -
@sorted_times.
when i do ls -l i get the following:
wxrwxr-x 1 radhika wheel 1028 Mar 24 11:57 chapter11_1.pl
-rwxrwxr-x 1 radhika wheel 387 Mar 24 12:37 chapter11_2.pl
-rwxrwxr-x 1 radhika wheel 551 Mar 24 21:25 chapter11_3.pl
-rwxrwxr-x 1 radhika wheel 281 Mar 15 16:28 file_basename.pl
-rw-rw-r-- 1 radhika wheel 236 Mar 21 11:46 filereader.pl
-rw-rw-r-- 1 radhika wheel 115 Mar 24 12:04 new.txt
-rw-rw-r-- 1 radhika wheel 115 Mar 24 12:00 rad.txt
-rwxrwxr-x 1 radhika wheel 648 Mar 15 15:40 rm_file_30days.pl
-rwxrwxr-x 1 radhika wheel 554 Mar 15 16:37 rmoldshares.pl
and when I execute the program I get the following:
radhika$ ./chapter11_3.pl *
Mon Mar 15 15:55:58 2004
Mon Mar 15 16:28:50 2004
Mon Mar 15 16:37:46 2004
Sun Mar 21 11:46:36 2004
Wed Mar 24 11:57:21 2004
Wed Mar 24 12:00:15 2004
Wed Mar 24 12:04:01 2004
Wed Mar 24 12:37:10 2004
Wed Mar 24 21:26:56 2004
my question is - where did the very first line from my output come from? ie
Mon Mar 15 15:55:58.
As you can see, ls -l does not show any file created at that time. Even . an
d .. are not the above time.
Is it using sort(@array), that sorts it in some manner that I do not underst
and?
Thanks,
Radhika
------
I've pasted the code for the script below.
----------------------------------------
#! /usr/bin/perl -w
use strict;
use diagnostics;
my $create_time;
my $i=0;
my @times;
my @sorted_times;
my $file;
foreach $file (@ARGV) {
my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $cti
me, $blksize, $blocks) = stat($file);
$create_time = localtime($ctime);
$times[$i] = $create_time;
$i++;
}
@sorted_times = sort(@times);
my $this_time;
foreach $this_time (@sorted_times) {
my($day, $mon, $dt, $tm, $yr) = split /\s+/,$this_time;
my ($hr, $mn, $sec) = split /:/, $tm;
print "$this_time\n";
}
Post Follow-up to this messageRadhika Sambamurti <radhika@88thstreet.com> wrote: : : my question is - where did the very first line from my : output come from? ie Mon Mar 15 15:55:58. As you can see, : ls -l does not show any file created at that time. Even : . and .. are not the above time. : Is it using sort(@array), that sorts it in some manner : that I do not understand? Have you read perlfunc? "The ctime field is non-portable, in particular you cannot expect it to be a 'creation time', see Files and Filesystems in the perlport manpage for details." HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
Post Follow-up to this messageUse this:
__BEGIN__
use strict;
use warnings;
die "No file name supplied.\n" unless @ARGV;
my $oldest_name = shift @ARGV;
my $oldest_age = -C $oldest_name;
foreach (@ARGV) {
my $age = -C;
($oldest_name, $oldest_age) = ($_, $age) if ($age > $oldest_age);
}
printf "The oldest file is '%s', and it is %.1f days old.\n",
$oldest_name, $oldest_age;
__END__
This is a modified version of the solution to exercise 11-3 in Learning Perl
(which I assume you are reading). The unmodified version uses '-M' not '-C'.
Radhika Sambamurti wrote:
> Hi,
> I have written a small script that is supposed to tell me the oldest file
in my directory (as per ctime). I have read the various times the various fi
les were created, into an array called times. I have then sorted this array
- @sorted_times.
> when i do ls -l i get the following:
>
> wxrwxr-x 1 radhika wheel 1028 Mar 24 11:57 chapter11_1.pl
> -rwxrwxr-x 1 radhika wheel 387 Mar 24 12:37 chapter11_2.pl
> -rwxrwxr-x 1 radhika wheel 551 Mar 24 21:25 chapter11_3.pl
> -rwxrwxr-x 1 radhika wheel 281 Mar 15 16:28 file_basename.pl
> -rw-rw-r-- 1 radhika wheel 236 Mar 21 11:46 filereader.pl
> -rw-rw-r-- 1 radhika wheel 115 Mar 24 12:04 new.txt
> -rw-rw-r-- 1 radhika wheel 115 Mar 24 12:00 rad.txt
> -rwxrwxr-x 1 radhika wheel 648 Mar 15 15:40 rm_file_30days.pl
> -rwxrwxr-x 1 radhika wheel 554 Mar 15 16:37 rmoldshares.pl
>
>
> and when I execute the program I get the following:
>
> radhika$ ./chapter11_3.pl *
> Mon Mar 15 15:55:58 2004
> Mon Mar 15 16:28:50 2004
> Mon Mar 15 16:37:46 2004
> Sun Mar 21 11:46:36 2004
> Wed Mar 24 11:57:21 2004
> Wed Mar 24 12:00:15 2004
> Wed Mar 24 12:04:01 2004
> Wed Mar 24 12:37:10 2004
> Wed Mar 24 21:26:56 2004
>
> my question is - where did the very first line from my output come from? i
e Mon Mar 15 15:55:58.
> As you can see, ls -l does not show any file created at that time. Even .
and .. are not the above time.
> Is it using sort(@array), that sorts it in some manner that I do not under
stand?
>
> Thanks,
> Radhika
> ------
> I've pasted the code for the script below.
> ----------------------------------------
> #! /usr/bin/perl -w
> use strict;
> use diagnostics;
>
> my $create_time;
> my $i=0;
> my @times;
> my @sorted_times;
> my $file;
>
> foreach $file (@ARGV) {
> my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $c
time, $blksize, $blocks) = stat($file);
> $create_time = localtime($ctime);
> $times[$i] = $create_time;
> $i++;
> }
> @sorted_times = sort(@times);
> my $this_time;
> foreach $this_time (@sorted_times) {
> my($day, $mon, $dt, $tm, $yr) = split /\s+/,$this_time;
> my ($hr, $mn, $sec) = split /:/, $tm;
> print "$this_time\n";
>
> }
>
>
--
Flemming Greve Skovengaard Be it the Devil or be it him
a.k.a Greven, TuxPower You can count on just one thing
<dsl58893@vip.cybercity.dk> When the time is up, you'll know
4168.08 BogoMIPS Not just one power runs the show
Post Follow-up to this messageRadhika Sambamurti wrote: > Hi, > I have written a small script that is supposed to tell me the oldest > file in my directory (as per ctime). I have read the various times > the various files were created, into an array called times. I have > then sorted this array - @sorted_times. when i do ls -l i get the > following: > [snip] ls is showing you mtime, not ctime. You should be using mtime. ctime is inode change time, NOT "create" time. Unix does not have a "create" time.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.