[TriLUG] `find -delete` not deleting

James Whisnant via TriLUG trilug at trilug.org
Tue Sep 6 15:03:49 EDT 2016


Here is a python script that should do what you want. You should only need
to uncomment the shutil.rmtree line once you are happy it behaves properly.

#remove.py

from datetime import datetime, timedelta
import os
import shutil

path = '/tmp/topdir'
too_old = 7

files = sorted(os.listdir(path))
for fn in files:
    fqn = os.path.join(path, fn)
    if os.path.isdir(fqn):
        if fn.startswith('201'): # fix me in 4 years
            when = datetime(int(fn[0:4]), int(fn[4:6]), int(fn[6:8]))
            if datetime.now() - when > timedelta(days=too_old):
                print 'would remove', fqn
                #shutil.rmtree(fqn) # does not work for symlinks
            else:
                print 'not removing', fqn

#$ python remove.py
#would remove /tmp/topdir/20160828
#not removing /tmp/topdir/20160906



On Tue, Sep 6, 2016 at 2:31 PM, Joseph Mack NA3T via TriLUG <
trilug at trilug.org> wrote:

> On Tue, 6 Sep 2016, Aaron Morrison via TriLUG wrote:
>
> Why are you using find? Your directories are already named for
>>> datestamps. Just have the cron job's shellscript calculate 7 days
>>> ago (let's say 8/27/2016), then do rm -rf whatever/20160827.
>>>
>>
> So what if cron misses its trigger?  E.g. On a laptop that is off or
>> sleeping?
>>
>
> This is it. It had to be an "older than" type instruction. You can't
> depend on a machine being up and running and everthing working all the time.
>
> Alan P said
>>
>
> lastweek=$(date -d "-7 days" +%y%m%d)
>>
>
> I notice that Alan uses these sort of date constructions all the time, but
> I find the documentation for the arguments to `date` hard to understand. I
> have to go find an example in my files somewhere, but they are few and far
> between. It's easier if I can find something else.
>
> Joe
>
> --
> Joseph Mack NA3T EME(B,D), FM05lw North Carolina
> jmack (at) austintek (dot) com - azimuthal equidistant
> map generator at http://www.wm7d.net/azproj.shtml
> Homepage http://www.austintek.com/ It's GNU/Linux!
> --
> This message was sent to: James Whisnant <jwhisnant at gmail.com>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  : http://www.trilug.org/mailman/
> options/trilug/jwhisnant%40gmail.com
> Welcome to TriLUG: http://trilug.org/welcome
>


More information about the TriLUG mailing list