Delete all files older than 1 day in directory

Discussion in 'Perl Misc' started by Andrew Thompson, Jan 12, 2004.

  1. Hey folks!

    New to perl programming, started (checks watch)
    3 hours ago.

    The task I am trying to achieve:
    Delete all folders within the 'public_compile'
    directory located at the root of my site.

    I understand from trawling through past posts to
    the group that the following should do it,..

    'for(</public_compile/*>){ unlink if -M>1 }',

    ...but I am somewhat nervous to give it to my
    server as a 'cron-job' since it deletes and uses
    a wild-card.

    I would test it on my local server but I cannot,
    for the life of me, figure how to set up a cron-job
    using the Apache 'manager' interface. [ On the live
    server thay are running cPanel - so simple even I
    can understand it (..mostly). ]

    So, the question (_finally_)..

    Will the line above have the effect I want?

    TIA

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Jan 12, 2004
    #1
    1. Advertising

  2. In article <17BMb.8249$>,
    Andrew Thompson <> wrote:
    :The task I am trying to achieve:
    :Delete all folders within the 'public_compile'
    :directory located at the root of my site.

    On a WWW site, right?


    :I understand from trawling through past posts to
    :the group that the following should do it,..

    :'for(</public_compile/*>){ unlink if -M>1 }',

    The leading / tells perl to look starting at the root of the
    filesystem, not at the root of the WWW site. Your code wouldn't
    work unless you've been chroot()'d.

    For example, on one of my WWW servers, my storage would be
    at /usr/people/roberson/public_html/, and my cgi scripts would
    live in /usr/people/roberson/cgi-bin/ and if I had a public_compile
    directory it would probably be
    /usr/people/roberson/public_html/public_compile . You script wouldn't
    be looking there, though, it would be looking at the system's
    /public_compile instead of in -my- public_compile directory.
    --
    Rome was built one paycheck at a time. -- Walter Roberson
     
    Walter Roberson, Jan 12, 2004
    #2
    1. Advertising

  3. Andrew Thompson

    Ben Morrow Guest

    "Andrew Thompson" <> wrote:
    > The task I am trying to achieve:
    > Delete all folders


    Directories. 'Folder' is baby-talk, suitable only for Apple users who
    don't know better.

    > within the 'public_compile' directory located at the root of my
    > site.


    Is there an extra constraint 'that are more than one day old'?

    > I understand from trawling through past posts to
    > the group that the following should do it,..
    >
    > 'for(</public_compile/*>){ unlink if -M>1 }',


    No, it won't. There are three things wrong:

    1. Your path is rooted at /, as Walter noted.
    2. You don't check which entries are directories.
    3. You can't just unlink a directory. You have to delete all the files
    in it and then rmdir it. If any of the files in it are directories,
    you have to recurse.

    Try (untested):

    use File::path qw/rmtree/;

    my $root = "/path/to/my/www";

    for (<$root/public_compile/*>) {
    -M > 1 or next;
    -d _ or next;
    rmtree $_, 0, 1;
    }

    > ..but I am somewhat nervous to give it to my
    > server as a 'cron-job' since it deletes and uses
    > a wild-card.


    Replace the 'rmtree' with a 'print' as a temporary measure, and check
    that the output you get from cron is what you were expecting. Then put
    the rmtree back in.

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Jan 12, 2004
    #3
  4. Andrew Thompson, Jan 13, 2004
    #4
  5. "Ben Morrow" <> wrote in message
    news:bturu1$bi0$...

    Way cool response Ben, saved for later
    close study. Especially liked the "Replace
    the 'rmtree' with a 'print'" bit.

    Thank you much.

    [ Oh, and yes 'lose the (sub)directory more
    than 1 day old' was it.. ]
     
    Andrew Thompson, Jan 13, 2004
    #5
    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. Prime
    Replies:
    6
    Views:
    1,943
  2. kbass
    Replies:
    2
    Views:
    1,893
    Graham Fawcett
    Nov 13, 2003
  3. Thomas W
    Replies:
    2
    Views:
    619
    Fredrik Lundh
    Mar 16, 2005
  4. Andy
    Replies:
    1
    Views:
    765
  5. SteveO
    Replies:
    12
    Views:
    363
    Tad McClellan
    Mar 7, 2006
Loading...

Share This Page