List/remove files, with filenames containing string that's "more than a month ago"?
- by Martin Tóth
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?