Mtime

Mtime

am 13.11.2007 22:44:35 von M

Is there an easy way to get perl to return the mtime of the oldest file in a
directory?

M

Re: Mtime

am 13.11.2007 22:49:09 von 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.


Abigail
--
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()

Re: Mtime

am 13.11.2007 23:06:13 von steveo

Abigail wrote:
> _
>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

Re: Mtime

am 13.11.2007 23:12:46 von Joost Diepenmaat

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.

Re: Mtime

am 14.11.2007 12:28:35 von Abigail

_
Steven M. O'Neill (steveo@panix.com) wrote on VCLXXXVII September
MCMXCIII in :
%% Abigail wrote:
%% > _
%% >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";

Re: Mtime

am 14.11.2007 14:14:56 von Peter Wyzl

"Steven M. O'Neill" wrote in message
news:fhd74l$lls$1@reader1.panix.com...
> Abigail wrote:
>> _
>>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

Re: Mtime

am 14.11.2007 15:15:03 von Glenn Jackman

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

Re: Mtime

am 14.11.2007 15:32:58 von Ben Morrow

Quoth "Peter Wyzl" :
> "Steven M. O'Neill" wrote in message
> news:fhd74l$lls$1@reader1.panix.com...
> > Abigail wrote:
> >>
> >>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