Shell Script to Delete 1 folder and keep 5

I have situation that I had originally thought would be easily remedied until I learned more about how -mtime actually works.

We have one server that collects backups from a number of other servers. The backup server is limited in space (for reasons that predate my employment). The standard here is to maintain 5 days worth of backup folders in each subdirectory.

Also- I should mention that this is a Windows server and I am using cygwin to run linux scripts.

For the purpose of example, here is the how the directory structure is setup;

g/backup/subdir

Within subdir, the backups directories simply have dates, so under subdir, you'd see;

2010-6-10 2010-6-11 2010-6-12 2010-6-13 2010-6-14 2010-6-15

The goal is to maintain 5 days and delete the 6th day's folder...

The script that I have setup to run against a directory like this is;

cd /cygdrive/g/backup/subdir
 
/usr/bin/find . -name "20*" -mtime -type d -execdir /usr/bin/rm -r '{}' \;
 

Now- here's the catch. The backup folders (2010-6-15) don't all get created at the exact same time. In fact for some, the times can vary greatly- from 7pm to 3am. This is where I think it breaks down when trying to use -mtime, since it's actually looking at the number of seconds, not the date on the folder.

I started trying to figure out a way of creating a file count variable, such as;

'dirnum=ls | wc -l'   #and then to go on and write an 'if/then' script like 
 
'if $dirnum > 5   then....'  

Again, I'm stuck with trying to figure out how to delete by date.

Sorry if this post is too wordy or if I've left out info. This marks my first post in a forum as I can usually figure these things out.

If anyone has any ideas or some better direction, it would be much appreciated.

thanks.

A point to start from (?):

#!/bin/bash

TO_KEEP=5
ls . | grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' >> BUFFER
ENTRIES=$( cat BUFFER | wc -l )
KICKOUT=$( cat BUFFER | sort -r | tail -n $(($ENTRIES-$TO_KEEP)) )
echo "$KICKOUT"
rm BUFFER

exit 0
# finis
[house@leonov] ls 2010*
2010-06-08/  2010-06-09/  2010-06-10/
2010-06-11/  2010-06-12/  2010-06-13/
[house@leonov] bash test.bash
2010-06-08

---------- Post updated 06-15-10 at 12:14 PM ---------- Previous update was 06-14-10 at 04:20 PM ----------

Actually- there is an issue with this. The way in which the backup folders are created are like the following;

2010-6-9/   2010-6-10/   2010-6-11/ 

The '6' in this was easily fixed by simply removing the {2} that was after it. (the only issue that results from this is what to do starting in October, but I will just create a second script when that time comes)

What to do with the '9' and then '10' . I've tried various expressions and wildcards, but cannot seem to get it to work. I have a test directory setup with directories just as above. It seems that whatever I try, the directories that echo as the 'last on the list' are 6-10 & 6-11, rather than 6-9 & 6-10.

thanks in advance.

---------- Post updated at 03:21 PM ---------- Previous update was at 12:14 PM ----------

I've actually have changed the list command to the following and it works. However, because of the digit placement of the 9 and the 10, the 9 is seen as the first digit, which messes up the sort command. This is an issue that I will need to resolve elsewhere.

My change;

ls . | grep -E '[0-9]{4}-[0-9]-[0-9]{0,}' >> BUFFER

I'd rather update the script creating the folders to use what I'd consider standard notation, read: 2 (or 4) digits for the year, 2 digits for every month and day, respectively.

I would have to agree. You're suggested script, with some minor changes, worked flawlessly (when the digits were correct). I'm looking into having the scripts that create the initial backup folders changed, to add another digit to the month/day where needed. Call it clean up work for the last admin.