If you just wanted to delete old files, you could simply use find with -mtime option to find files of certain age and with -exec option to execute rm on them, with needed options. If you insist on a script, I'll throw in some simple example which you can start working on...I'll assume the directories are indeed called data-YYYYMMDD as in your post, and they reside in the working directory (which you could easily change, of course).
Code:
#!/bin/bash
today=$(date -j -f "%a %b %d %T %Z %Y" "$(date)" "+%s")
month=2592000
reference=$(($today-$month))
filearray=(data-*)
for i in ${filearray[@]}
do
num=$(date -j -f "%Y%m%d" ${i:5} "+%s")
if [ $num -lt $reference ]
then
echo "$i is older than $reference"
else
echo "$i is not older than $reference"
fi
done
What this should do (I did only few tests after writing this, so be aware!) is
1) take the current date and transform it into Epoch (number of seconds since UTC midnight Jan 1 1970)
2) calculate reference value that is 30 days older than that of today (30*24*60*60 seconds)
2) collect all files (directories) whose name begins with "data-" into an array
3) work through the array
- pick up the numerical date-part of the name of each element and transform it into Epoch
- compare the current value against the reference value, checking if it's smaller (=older) or not than the reference
- do something in either case, in the example simply echo the result.
Remember: this relies on the date formats being what they are thought to be. Make sure that it works the way you want before allowing it to remove anything. In fact, rather than directly delete, I'd personally make it
move the to-be-removed directories into a "trash directory" first, and empty that a few days later. This would give you an opportunity to save files in case something went wrong.
This is a really simplistic example, but you can (and should) modify it to your needs. I suspect that other languages, for example Perl, would make this a lot easier--or then it's just me, but you insisted on bash. Replace the echo statements with actual working stuff (like rm), build a test case (so as not to destroy valuable data in case it does gimmicks), test, and so on.
Hope it helped a little.