Mtime
am 13.11.2007 22:44:35 von MIs there an easy way to get perl to return the mtime of the oldest file in a
directory?
M
Is there an easy way to get perl to return the mtime of the oldest file in a
directory?
M
_
M (no@spam.tonsjunkmail.com) wrote on VCLXXXVII September MCMXCIII in
|| Is there an easy way to get perl to return the mtime of the oldest file in a
|| directory?
No.
Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that
for some definitions of "oldest", including the very obvious one, you need
a piece of information that many filesystems don't keep track of.
None of these reasons have anything to do with the language you try to
solve the problem with.
Abigail
--
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
Abigail
> _
>M (no@spam.tonsjunkmail.com) wrote on VCLXXXVII September MCMXCIII in
>
>|| Is there an easy way to get perl to return the mtime of the oldest file in a
>|| directory?
>
>
>No.
>
>Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that
>for some definitions of "oldest", including the very obvious one, you need
>a piece of information that many filesystems don't keep track of.
>
>None of these reasons have anything to do with the language you try to
>solve the problem with.
http://groups.google.com/group/comp.unix.shell/msg/777922ad1 6818eec?dmode=source&hl=en
--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo
On Tue, 13 Nov 2007 15:44:35 -0600, M wrote:
> Is there an easy way to get perl to return the mtime of the oldest file
> in a directory?
>
> M
Depends on what you call the oldest file. Many file systems don't have a
concept of oldest file (for IMHO good reasons, regardless of Abigail's
comments).
see "perldoc -f readdir" and "perldoc -f -X" (especially -M and -C).
Also, looking for inode in your favorite Unix manual (or google) may be
helpful.
Joost.
_
Steven M. O'Neill (steveo@panix.com) wrote on VCLXXXVII September
MCMXCIII in
%% Abigail
%% > _
%% >M (no@spam.tonsjunkmail.com) wrote on VCLXXXVII September MCMXCIII in
%% >
%% >|| Is there an easy way to get perl to return the mtime of the oldest file in a
%% >|| directory?
%% >
%% >
%% >No.
%% >
%% >Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that
%% >for some definitions of "oldest", including the very obvious one, you need
%% >a piece of information that many filesystems don't keep track of.
%% >
%% >None of these reasons have anything to do with the language you try to
%% >solve the problem with.
%%
%% http://groups.google.com/group/comp.unix.shell/msg/777922ad1 6818eec?dmode=source&hl=en
%%
I saved the program Tom wrote, and saved it in "/tmp/tom".
Then I ran the following program:
#!/usr/bin/perl
use strict;
use warnings;
no warnings 'syntax';
my $DIR = "/tmp/test";
die "$DIR already exists\n" if -f $DIR;
mkdir $DIR, 0755 or die "mkdir: $!";
open my $fh1 => ">", "$DIR/oldest" or die "open: $!";
print $fh1 "Hello\n";
close $fh1 or die "close $!";
sleep 10;
open my $fh2 => ">", "$DIR/newest" or die "open: $!";
print $fh2 "Hello\n";
close $fh2 or die "close $!";
sleep 10;
open my $fh3 => ">>", "$DIR/oldest" or die "open: $!";
print $fh3 "world\n";
close $fh3 or die "close: $!";
system perl => "/tmp/tom", $DIR;
system rm => '-r', $DIR;
__END__
Its output:
oldest file in /tmp/test is /tmp/test/newest, time is Wed Nov 14 12:22:53 2007
Hmmm, /tmp/test/oldest was created before /tmp/test/newest, so, arguebly,
it's the oldest file in the directory.
But Tom's program didn't return it's mtime; it returned the mtime of the
newest file.
Getting the mtime of a file is easy. Getting the mtimes of all the files
in a directory is easy as well, and selecting the oldest mtime from the
set is trivial as well.
But what is the "oldest" file? The first one created? The last one modified?
The first entry in the directory? Determining what "oldest" stands for,
that's the hard part of the original question.
Abigail
--
use lib sub {($\) = split /\./ => pop; print $"};
eval "use Just" || eval "use another" || eval "use Perl" || eval "use Hacker";
"Steven M. O'Neill"
news:fhd74l$lls$1@reader1.panix.com...
> Abigail
>> _
>>M (no@spam.tonsjunkmail.com) wrote on VCLXXXVII September MCMXCIII in
>>
>>|| Is there an easy way to get perl to return the mtime of the oldest
>>file in a
>>|| directory?
>>
>>
>>No.
>>
>>Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that
>>for some definitions of "oldest", including the very obvious one, you need
>>a piece of information that many filesystems don't keep track of.
>>
>>None of these reasons have anything to do with the language you try to
>>solve the problem with.
>
> http://groups.google.com/group/comp.unix.shell/msg/777922ad1 6818eec?dmode=source&hl=en
Wow, thats what? 14 years ago?
Abigail's comments, albeit crytic are correct. Which definition of oldest
do you require? Least recently modified? Least recently accessed? First
created?
Once you answer that, the rest is trivial with 'stat' and 'readdir'...
P
At 2007-11-13 04:44PM, "M" wrote:
> Is there an easy way to get perl to return the mtime of the oldest file in a
> directory?
opendir my $dir, '.' or die "...";
my @files = readdir $dir;
closedir $dir;
my @with_mtime = map {[$_, (stat $_)[9]]} @files;
my @sorted = sort {$a->[1] <=> $b->[1]} @with_mtime;
my $oldest = $sorted[0][0];
You can avoid all the temp vars:
opendir my $dir, "." or die "...";
my $oldest = [ sort {$a->[1] <=> $b->[1]}
map {[$_, (stat $_)[9]]}
readdir $dir
]->[0][0];
closedir $dir;
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Quoth "Peter Wyzl"
> "Steven M. O'Neill"
> news:fhd74l$lls$1@reader1.panix.com...
> > Abigail
> >>
> >>Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that
> >>for some definitions of "oldest", including the very obvious one, you need
> >>a piece of information that many filesystems don't keep track of.
>
> Abigail's comments, albeit crytic are correct. Which definition of oldest
> do you require? Least recently modified? Least recently accessed? First
> created?
>
> Once you answer that, the rest is trivial with 'stat' and 'readdir'...
Part of Abigail's point was that 'first created' is impossible to
determine on many filesystems, as creation time is not recorded. Even
when it is, Perl may not provide easy access to that information (BSD's
'birthtime', for instance).
Ben