Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding newest file in a directory and removing the rest

Reply
Thread Tools

finding newest file in a directory and removing the rest

 
 
Stu
Guest
Posts: n/a
 
      10-10-2008
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-10-2008
Stu <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      10-10-2008
Stu <(E-Mail Removed)> 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/"
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-10-2008

Quoth Stu <(E-Mail Removed)>:
> 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 [(E-Mail Removed)]
 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      10-13-2008
On Oct 10, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
> Stu <(E-Mail Removed)> 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/"


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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-13-2008
Stu <(E-Mail Removed)> wrote:
> On Oct 10, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
>> Stu <(E-Mail Removed)> 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/*';


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-13-2008
Stu <(E-Mail Removed)> 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
 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      10-13-2008
On Oct 13, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
> Stu <(E-Mail Removed)> wrote:
> > On Oct 10, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
> >> Stu <(E-Mail Removed)> 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/*';
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
>
> - Show quoted text -


Thanks for all your help. That was it
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-13-2008
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."
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-13-2008
Stu <(E-Mail Removed)> wrote:
> On Oct 13, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
>> Stu <(E-Mail Removed)> wrote:
>> > On Oct 10, 12:14*pm, Tad J McClellan <(E-Mail Removed)> wrote:
>> >> Stu <(E-Mail Removed)> 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...


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

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
Show newest file in directory? Mmcolli00 Mom Ruby 6 12-19-2008 08:15 AM
reading a directory, first files the newest ones jordilin Perl Misc 17 10-29-2007 11:07 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Finding the newest directory sc0ri0n Perl Misc 6 05-13-2004 07:48 AM



Advertisments