Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

finding newest file in a directory and removing the rest

1 view
Skip to first unread message

Stu

unread,
Oct 10, 2008, 10:27:35 AM10/10/08
to
I was wondering if somebody can point me in the right direction?

I have a requirement to remove all the files in a given directory
except for the one that has been
created last. In ksh, to find this file I would not remove I would do
a ls -ltr | tail -1. Can somebody provide me with a function that can
do something similiar but with perl code.

Thanks in advance to all who answer this post

Jürgen Exner

unread,
Oct 10, 2008, 11:45:02 AM10/10/08
to
Stu <beefs...@hotmail.com> wrote:
>I have a requirement to remove all the files in a given directory
>except for the one that has been
>created last. In ksh, to find this file I would not remove I would do
>a ls -ltr | tail -1. Can somebody provide me with a function that can
>do something similiar but with perl code.

perldoc -f opendir
perldoc -f readdir (or instead perldoc -f glob)
perldoc -f -M (for a finer granularity see perldoc -f stat); Please be
advised that this provides the inode change time which may or may not
coincide with the file creating time. However this is the best guess on
many (most?) file systems because they don't track file creation time.
Besides, "creation time" is ambigious at best. If you copy a photo from
the memory card to you computer, at what moment was that file created:
when you hit the shutter or when you copied the file?
perldoc -f sort
perldoc -f unlink

jue

Ben Morrow

unread,
Oct 10, 2008, 12:14:16 PM10/10/08
to

Quoth Stu <beefs...@hotmail.com>:

> I was wondering if somebody can point me in the right direction?
>
> I have a requirement to remove all the files in a given directory
> except for the one that has been
> created last. In ksh, to find this file I would not remove I would do
> a ls -ltr | tail -1. Can somebody provide me with a function that can
> do something similiar but with perl code.

perldoc -f readdir
perldoc -f stat
Maybe perldoc -f -X
perldoc -f sort
perldoc File::stat

Ben

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [b...@morrow.me.uk]

Tad J McClellan

unread,
Oct 10, 2008, 12:14:14 PM10/10/08
to
Stu <beefs...@hotmail.com> wrote:
> I was wondering if somebody can point me in the right direction?
>
> I have a requirement to remove all the files in a given directory
> except for the one that has been
> created last.


That is only possible on file systems that keep track of
a file's creation time.

Most *nix filesystems do not record creation time at all.


If you can live with modification time instead of creation time,
then this should do it:

my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
unlink @f;


(but that does not remove all old files, old files that start
with a dot will be left alone.
)

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Stu

unread,
Oct 13, 2008, 12:12:52 PM10/13/08
to
On Oct 10, 12:14 pm, Tad J McClellan <ta...@seesig.invalid> wrote:

Thanks for the response but I am not too sure I understand what this
statement is doing.

When I do this:

@f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
*>;
foreach $file (@f)
{
print $file . "\n";
}

I get I only get one file as the output yet there are several files in
the above-mentioned directory. I would have thought I would have
gotten back all the files in the directory in sorted order by modtime
or creation time.

The file I did get back appears to have a time associated with it that
is in the middle of all my files

Oct 1 15:08 newest file
Sep 19 10:40 time associated with the file that was returned
Jul 18 11:21 older file

How come the older or the newer file was not returned?

Once again thanks for the help

Tad J McClellan

unread,
Oct 13, 2008, 12:14:31 PM10/13/08
to
Stu <beefs...@hotmail.com> wrote:
> On Oct 10, 12:14 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
>> Stu <beefstu...@hotmail.com> wrote:
>> > I was wondering if somebody can point me in the right direction?
>>
>> > I have a requirement to remove all the files in a given directory
>> > except for the one that has been
>> > created last.
>>
>> That is only possible on file systems that keep track of
>> a file's creation time.
>>
>> Most *nix filesystems do not record creation time at all.
>>
>> If you can live with modification time instead of creation time,
>> then this should do it:
>>
>>     my(undef, @f) = sort { -M $a <=> -M $b } grep -f, glob '*';
>>     unlink @f;
>>
>> (but that does not remove all old files, old files that start
>>  with a dot will be left alone.
>> )
>>
>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


It is bad form to quote .sigs. Please do not do that.

You should also trim irrelevant text, and interleave your comments.

If you are going to comment on one line of code, then you should
quote only the one line of code that you are going to comment on.


> Thanks for the response but I am not too sure I understand what this
> statement is doing.
>
> When I do this:
>
> @f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
> *>;


You are calling glob() *twice*.

Once with its name, and once with its alternate form.

Call it only once instead:

@f = sort { -M $a <=> -M $b } grep -f, </axsma/pbh/var/proc/pbh40/*>;
or, better
@f = sort { -M $a <=> -M $b } grep -f, glob '/axsma/pbh/var/proc/pbh40/*';

Jürgen Exner

unread,
Oct 13, 2008, 12:36:29 PM10/13/08
to
Stu <beefs...@hotmail.com> wrote:
>@f = sort { -M $a <=> -M $b } grep -f, glob </axsma/pbh/var/proc/pbh40/
>*>;
>
>I get I only get one file as the output yet there are several files in
>the above-mentioned directory. I would have thought I would have

This is a neat one :-))

You are double globbing. Either drop the glob() call or replace the
angle brackets with paranthesis.

jue

Stu

unread,
Oct 13, 2008, 12:40:59 PM10/13/08
to
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
>
> - Show quoted text -

Thanks for all your help. That was it

Dr.Ruud

unread,
Oct 13, 2008, 3:11:58 PM10/13/08
to
Stu schreef:

>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
>
> Thanks for the response but I am not too sure I understand what this
> statement is doing.

It prints an address.

--
Affijn, Ruud

"Gewoon is een tijger."

Tad J McClellan

unread,
Oct 13, 2008, 6:47:41 PM10/13/08
to
Dr.Ruud <rvtol...@isolution.nl> wrote:
> Stu schreef:
>
>>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
>>
>> Thanks for the response but I am not too sure I understand what this
>> statement is doing.
>
> It prints an address.


<grin>


--
Tad McClellan

Tad J McClellan

unread,
Oct 13, 2008, 6:46:46 PM10/13/08
to
Stu <beefs...@hotmail.com> wrote:
> On Oct 13, 12:14 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
>> Stu <beefstu...@hotmail.com> wrote:
>> > On Oct 10, 12:14 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
>> >> Stu <beefstu...@hotmail.com> wrote:


[ snip 20 lines ]


>> >> --
>> >> Tad McClellan
>> >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
>>
>> It is bad form to quote .sigs. Please do not do that.
>>
>> You should also trim irrelevant text, and interleave your comments.


[ snip 20 lines ]


>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks for all your help.


Yeah, right.

Off to perpetual invisibility you go...

0 new messages