Delete files based on date – ksh bash

Shell script for AIX systems to automatically delete old TSM disaster recovery plan files based on their modification date.
Referring to an earlier blog post on how to delete log files or DR Plans using PowerShell I have another TSM server that is running on AIX. It’s new so I don’t have a bunch of DR plans in the directory currently. Also this is a server I use for my internal testing and development so it’s not too heavily used by others.
The Problem
DR plan files accumulate over time and need to be cleaned up regularly to prevent disk space issues. Unlike Windows PowerShell, on AIX we need to use shell scripting with standard UNIX commands.
The Solution
I set it to only keep 7 days worth of files and now all I need to do is put it to run in my crontab every day.
#!/usr/bin/ksh
DRM_Directory=/opt/tivoli/storage/drm/drplan
DaysToKeep=7
find $DRM_Directory -type f -mtime +$DaysToKeep -exec rm -f {} \;
How it Works
-
find
searches for files in the specified directory -
-type f
limits the search to files only (not directories) -
-mtime +$DaysToKeep
finds files older than the specified number of days -
-exec rm -f {} \;
executes therm
command on each found file
This simple script keeps your DR plan directory clean by automatically removing files older than the retention period. Perfect for running as a daily cron job.
Cheers!