Howto self-delete files periodically with Perl

G

gundalav

Dear expert,

Suppose I have a file created in this way:

~/MyDir/ $ perl mycode.pl > subdir/the_result.out

What I desired to do is to have a
script that : "automatically delete the files after 10 days they are
created".

What's the effective strategy to do this in Perl?
Or perhaps is there a better non-Perl solution?

Regards,
Gundala
 
M

Marcel Müller

gundalav said:
What's the effective strategy to do this in Perl?

Look for File::Find and stat.
Or perhaps is there a better non-Perl solution?

There is no need to do so.
However, on Unix-like systems you will manage this with find and rm too.


Marcel
 
T

Tad McClellan

gundalav said:
What I desired to do is to have a
script that : "automatically delete the files after 10 days they are
created".


That is not possible on many filesystems.

Unixy filesystems for instance, do not keep track of when a
file is created.

So, I will assume you meant "10 days they are last accessed" instead.

What's the effective strategy to do this in Perl?


perl -le 'for (@ARGV) {print $_ if -A > 10.0}' *


Change 'print' to 'unlink' once you are convinced that it
is working OK.
 
X

xhoster

gundalav said:
Dear expert,

Suppose I have a file created in this way:

~/MyDir/ $ perl mycode.pl > subdir/the_result.out

Presumably you have lots of files created this way, otherwise this seem
pretty trivial. OTOH, what you show is only capable of making one file,
as it just uses the same name over and over. So I don't think you've
described your problem well or completely.
What I desired to do is to have a
script that : "automatically delete the files after 10 days they are
created".

Is "created" important, as opposed to modified or accessed?

The obvious answer to your question doesn't depend on *how* the file was
created, which (combined with your subject line) makes me wonder of you
meant to ask something different than what you did ask.

Is the program supposed to remember what files it created, so that it knows
which ones are elligible for deletion after 10 days?

Xho
 
U

usenet

gundalav said:
script that : "automatically delete the files after 10 days they are created".

Your operating system doesn't know when a file was created, so Perl
doesn't know either (unless you maintain an activity log or database or
something that Perl can go to to find this information - or you could
embed the creation date as part of the filename).

The "ctime" key on the inode (available via stat) is NOT creation time
("c" stands for "change", not "create", and the value of this key
refers to the last time the file's inode was changed and will usually
match the mtime value).
 
G

gundalav

Thanks a lot for the reply Tad.
Few things....
perl -le 'for (@ARGV) {print $_ if -A > 10.0}' *

What does -A stands for? Where can I find
information about this? Is there a way also to print out
the actual date (just like when we do unix's "ls -l" ) under Perl
one-liner and coupled with -A option ?

Hope to hear from you again.

Regards,
Gundala
 
T

Tad McClellan

gundalav said:
Thanks a lot for the reply Tad.


Please do not send stealth Cc's, as that is bad netiquette.

Few things....


What does -A stands for? Where can I find
information about this?


perldoc -f -A

Is there a way also to print out
the actual date


perldoc -f stat # to get the file's timestamp

perldoc -f localtime # to make it into a human-readable date
 
B

Ben Morrow

Quoth (e-mail address removed):
perldoc -f stat # to get the file's timestamp

You may also want to look aht the File::stat module, which provides a
much nicer interface to the same information.

Ben
 
C

cmic

Hi Gundalav

gundalav a écrit :
Dear expert,

Suppose I have a file created in this way:

~/MyDir/ $ perl mycode.pl > subdir/the_result.out

What I desired to do is to have a
script that : "automatically delete the files after 10 days they are
created".

Why don't you use the Unix idiom (instead of Perl) :

prompt> find /mydir -ctime +5 -exec rm {} \;

Note that ctime is the modifaication time of file status, which is not
always the same as the creation time. (already noted by Filmer).
Sorry for this Out of topic...
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top