Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Delete all files older than 1 day in directory

Reply
Thread Tools

Delete all files older than 1 day in directory

 
 
Andrew Thompson
Guest
Posts: n/a
 
      01-12-2004
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


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-12-2004
In article <17BMb.8249$(E-Mail Removed)>,
Andrew Thompson <(E-Mail Removed)> wrote:
:The task I am trying to achieve:
elete 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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-12-2004

"Andrew Thompson" <(E-Mail Removed)> 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:ath 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.
~ http://www.velocityreviews.com/forums/(E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-13-2004
"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
news:btunif$ngs$(E-Mail Removed)...
....
| elete all folders within the 'public_compile'
.....
| On a WWW site, right?

Sorry, literally: http://www.physci.org/public_compile/

And, ..thank you. Your answer pretty
much clarified everything I needed.

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


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-13-2004
"Ben Morrow" <(E-Mail Removed)> wrote in message
news:bturu1$bi0$(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Now.day and day(now) returning the wrong day! Andy ASP .Net 1 11-06-2008 12:20 PM
Using File::Find to delete files and directories older than 30 days SteveO Perl Misc 12 03-07-2006 09:00 PM
Program to list all dirs, subdirs and files older than 1 year Prime Java 6 12-15-2005 08:56 AM
Downloading all files older than 3 hours from a ftp-server. Thomas W Python 2 03-16-2005 01:47 PM
Newby Question: Remove files older than 7 days from a directory kbass Python 2 11-13-2003 07:52 PM



Advertisments