http://qs321.pair.com?node_id=28914


in reply to Find files older than x days and delete them

Here is a script I have used for a similar task in one directory. I have changed the name of the directory for obvious reasons:
#! /usr/local/bin/perl die unless chdir "/some/hardcoded/dir/here"; die unless opendir DIR, "."; foreach $file (grep {-f && (14 < -M)} readdir DIR) { unlink $file; } closedir DIR;
Amusing note. I wrote this after accidentally crashing someone else's ftp server by throwing the accumulated contents of a large dir at it. The next day I went and made every change I could think of that would have prevented the disaster. Running this script daily was one of those changes. :-)

EDIT
Hmmm...this was one of my earlier scripts. Definitely before I learned the value of informative error messages. And written in a hurry...but I think I should leave it anyways as an example to show that my coding style has matured...

Replies are listed 'Best First'.
RE: RE (tilly) 1: Find files older than x days and delete them
by merlyn (Sage) on Aug 22, 2000 at 01:32 UTC
      I know this is plain, flat heresy, especially here inside the monastry, but why use perl for this at all?
      find /your/path -mtime numberofdays -exec rm -f {} \;
      Father, forgive me, for I have sinned ... ;-))

      Andreas

      Update:

      Once again I am humbly admitting that merlyn and tilly made good points (see below). So now we have three chief reasons (weapons) for a perl solution:

      After this confession, shall I be free? Hey, stick away from that comfy chair ...
      (do you know that feeling when you should do your already late travel and time reports for your company but keep lurking around instead? Oh boy ...)
        Portability? :-)
        I use it b/c there are several file types that I'm looking for, and I need a log. Windows Find doesn't log, and I'm not sure that you can search out multiple file types. Plus, I'm lazy. I like one click.