Python Linux Find Files With Pattern Accessed Older Than N Days And Remove
This is a neat utility that you can use to keep in your sysadmin bag of tricks, it walks the directory you define recursively and grabs all the file access times and stores them into a list, it then compares them against a command line parameter for days ago. If its older than N days it will remove the file. What's really nice about this utility is it has a debug mode, this way you can see what will be deleted before you remove debug and execute it.
#!/usr/bin/env python3
import argparse
import fnmatch
import os
import sys
from datetime import datetime, timedelta
from pathlib import Path
# set date now.
now = datetime.today()
# setup dir to clean
home = str(Path.home())
target_dir = '/home/jasonr' # CHANGE TO WHERE YOU WANT TO SEARCH
# dir to clean
dirs_to_clean = target_dir
# setup cli arguments.
parser = argparse.ArgumentParser(
description='''
[--days_ago 60] will keep 60 days worth of files.
[--debug yes] will print out statements with no actions.''',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--days_ago',
help='[--days_ago NN]')
parser.add_argument('--debug',
help='[--debug (yes|no)')
args = parser.parse_args()
# allowed arguments from cli.
accepted_cli_args = ['yes', 'no']
# sanity check, assign days to keep on system.
if args.days_ago is None:
days = 60
else:
days = args.days_ago
# define a list of patterns
patterns = ['*.csv', '*.txt'] # YOU CAN ADD ANY PATTERN TO LIST
# sanity check, assign debug true or false
if args.debug in accepted_cli_args:
if args.debug == 'yes':
debug = True
else:
debug = False
else:
print("{0}: Wrong parameter --debug (yes or no): [{1}]"
.format(now, args.debug))
sys.exit(1)
def find_files(dir_to_clean):
file_list = []
days_ago = datetime.now() - timedelta(days=int(days))
for root, dirs, files in os.walk(dir_to_clean):
for pattern in patterns:
for filename in fnmatch.filter(files, pattern):
file_list.append(os.path.join(root, filename))
file_list.sort()
for file in file_list:
try:
file_atime = datetime.fromtimestamp(os.path.getatime(file))
except Exception as e:
print("{0}: File Access Time Get Failed: [{1}]"
.format(now, e))
if file_atime < days_ago:
if os.path.isfile(file):
try:
if not debug:
print("{0}: Removing file: [{1}]"
.format(now, file))
os.remove(file)
else:
print("{0}: DEBUG: Removing file: [{1}]"
.format(now, file))
except OSError as e:
print("{0}: File Clean Up Failed: [{1}]"
.format(now, e))
sys.exit(1)
# main function.
def main():
find_files(dirs_to_clean)
if __name__ == "__main__":
main()
[jasonr@sb-jralph-8 ~]$ python3 finder.py --days_ago 90 --debug yes
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/awscli/examples/emr/create-cluster-synopsis.txt]
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/cryptography-3.3.2-py3.8.egg-info/top_level.txt]
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/docutils/parsers/rst/include/README.txt]
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/docutils/parsers/rst/include/isoamsa.txt]
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/docutils/parsers/rst/include/isoamsb.txt]
2022-07-07 11:22:57.524454: DEBUG: Removing file: [/home/jasonr/aws/dist/docutils/parsers/rst/include/isoamsc.txt]