Delete all files older than 1 day in directory

A

Andrew Thompson

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
 
W

Walter Roberson

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

Ben Morrow

Andrew Thompson said:
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
 
A

Andrew Thompson

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

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,041
Latest member
RomeoFarnh

Latest Threads

Top