Howto self-delete files periodically with Perl

Discussion in 'Perl Misc' started by gundalav, Jun 5, 2006.

  1. gundalav

    gundalav Guest

    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
    gundalav, Jun 5, 2006
    #1
    1. Advertising

  2. gundalav wrote:
    > 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
    Marcel Müller, Jun 5, 2006
    #2
    1. Advertising

  3. gundalav <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 5, 2006
    #3
  4. gundalav

    Guest

    "gundalav" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 5, 2006
    #4
  5. gundalav

    Guest

    gundalav wrote:
    > 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).

    --
    David Filmer (http://DavidFilmer.com)
    , Jun 5, 2006
    #5
  6. gundalav

    gundalav Guest

    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
    gundalav, Jun 6, 2006
    #6
  7. gundalav

    Scott Bryce Guest

    Scott Bryce, Jun 6, 2006
    #7
  8. gundalav <> wrote:

    > Thanks a lot for the reply Tad.



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


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

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 6, 2006
    #8
  9. gundalav

    Ben Morrow Guest

    Quoth :
    > gundalav <> wrote:
    >
    > > Is there a way also to print out
    > > the actual date

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

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    (Kate Rusby)
    Ben Morrow, Jun 6, 2006
    #9
  10. gundalav

    cmic Guest

    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...
    --
    cmic
    >
    > What's the effective strategy to do this in Perl?
    > Or perhaps is there a better non-Perl solution?
    >
    > Regards,
    > Gundala
    cmic, Jun 6, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    561
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    580
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    390
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    361
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    386
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page