I would like to delete the oldest files in a directory, after a limit of 100 files. In other words, I want to ensure that no more than 100 files exist in the directory, and if a limit is exceeded, delete the oldest files after the limit. I don't just want to delete files older than x days, since if this was run on a cronjob, eventually all files would be deleted.
I guess if I were to program this, the pseudo code would be:
list = dir.getFiles()
list.sortByDate()
deleteList = list.getSubList(100, end) // from, to
deleteAll(deleteList)
So what would the appropriate Unix command be? I guess find would be involved somehow with the -exec argument, but I'm not sure about the sorting/limiting aspect.