Delete oldest files/directories - but not all!
Posted on 2003-12-10
I have an automated process that deposits generated files in subdirs (that it creates) of a fixed directory. So it looks like
What I want to do is setup a cron job that will clean up the mydir directory. Here's how I want it to work:
I want to delete all the directories (the 20031203-type, not mydir), and their files, EXCEPT for the newest 10. So if there's only 10 dirs or less when the cleanup script runs, it does nothing. If there are 33 dirs, it deletes the 23 oldest.
I'm trying to do this with ls -c, and then pipe through head and/or tail, but I just can't see a way to do it that way.
How can I write a script that will do this?