Sweet Little Data File Retention Script
One of the things I wanted to get done this afternoon was to clean up the bash script I have that runs every night out of cron to clean up (delete) the data files that I can't afford to keep. These are the data files for me feed recorders, and I can afford to keep a few days, but not a lot. The old code I had was really pretty lame - and it scanned the directory looking for subdirectories and looking for the highest sorted name, and picking that one as the "latest".
I did this several times, each pass getting the next one, and the next one, and in the end, I had a list of directories to "keep". Very sloppy. Here's what I came up with this afternoon after just a little bit of fiddling:
i=0 for d in `ls | sort -r` do # skip anything that's not a directory if [ ! -d ${d} ]; then continue fi # track count of dirs to keep - and keep 4 i=$(($i + 1)) if [ $i -le 4 ]; then continue fi # whatever is left - deleteā¦ it's too old echo -n '.' rm -rf ${d} done
The beauty of this script is that I can easily change the number of days to keep - just change the '4' to a '5', and we have a whole (business) week. Very nice. Also, it's a lot more compact than the original approach.