BASH script that removes sub-directories (not just files) but keeps the two newest.
Hello. This is my first posting and I'm stuck trying to script this logic together.
The task is to write a script that can be run from cron (preferably bash) that will recursively delete directories as a cleanup task. That part I can do without too much difficulty thanks to the many examples on this forum. However, an added piece is that I need the script to leave the two *newest* created directories intact. The objective is to always have the two newest directories available, but prune older ones as time goes on.
If the script is run against a directory that only has two sub-directories, it should exit and do nothing.
Any help would be appreciated. I'm working on FC8.