Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How do I find and delete files based on age?

by duckyd (Hermit)
on Feb 26, 2007 at 20:44 UTC ( [id://602213] : note . print w/replies, xml ) Need Help??


in reply to How do I find and delete files based on age?

If your task really is as simple as you describe (and you don't anticipate it becoming more complicated later on) then there's no reason not to just use find:

find ./ -name 'DATE_*' -mtime +14 -exec rm -rf {} \;

backup first, test before you run (w/o the -exec rm -rf {} \;) to verify it finds the right fields, etc, etc...

Replies are listed 'Best First'.
Re^2: How do I find and delete files based on age?
by bsdz (Friar) on Feb 27, 2007 at 14:17 UTC
    I have never used it but I believe find2perl will convert the above command into pure perl. Just another option :)
Re^2: How do I find and delete files based on age?
by 0xbeef (Hermit) on Feb 28, 2007 at 08:41 UTC
    The use of a relative path is a good thing, but this is incomplete. Paranoia should take control of when you use a destructive command and you should never make assumptions.

    Here is a simplified example where the tests are inadequate:

    cd /targetdir/targetsubdir rm -fr *

    Imagine if the target directory was not mounted, or your chdir failed for whatever reason (e.g. inadequate permissions). Yes, you are likely now listening to the whirr of you hard-disk working feverishly to delete everything from the directory you were in prior to the failed cd, and I have personally witnessed cases of that particular directory being / .

    The safe approach is:

    > cd $TARGETDIR && rm -fr ./targetsubdir or > test -d $TARGETDIR && find . -name 'DATE_*' -type f -mtime +14 -exec + rm -fr {} \;

    Niel