finding newest file in a directory and removing the rest

S

Stu

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

Jürgen Exner

Stu said:
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
 
T

Tad J McClellan

Stu said:
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.
)
 
B

Ben Morrow

Quoth Stu said:
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
 
S

Stu

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.
)

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
 
T

Tad J McClellan

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

Jürgen Exner

Stu said:
@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
 
S

Stu

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.




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top