List/remove files, with filenames containing string that's "more than a month ago"?
Posted
by
Martin Tóth
on Super User
See other posts from Super User
or by Martin Tóth
Published on 2011-01-12T16:54:45Z
Indexed on
2011/01/12
17:56 UTC
Read the original article
Hit count: 280
I store some data in files which follow this naming convention:
/interesting/data/filename-YYYY-MM-DD-HH-MM
How do I look for the ones with date in file name < now - 1 month
and delete them?
Files may have changed since they were created, so searching according to last modification date
is not good.
What I'm doing now, is filter-ing them in python:
prefix = '/interesting/data/filename-'
import commands
names = commands.getoutput('ls {0}*'.format(prefix)).splitlines()
from datetime import datetime, timedelta
all_files = map(lambda name: {
'name': name,
'date': datetime.strptime(name, '{0}%Y-%m-%d-%H-%M'.format(prefix))
}, names)
month = datetime.now() - timedelta(days = 30)
to_delete = filter(lambda item: item['date'] < month, all_files)
import os
map(os.remove, to_delete)
Is there a (oneliner) bash
solution for this?
© Super User or respective owner