So, assuming they're all in the same directory.
I'll point out I tested portions of this script, but not all of it, so you'll probably want to do some modification/testing before you actually run it - lest you end up with all your directories removed.
# Find the date 30 days ago.
recent_dirs=`date -d "-30 days" +%d-%m-%Y`
rec_month=`echo "${recent_dirs}" | cut -d '-' -f2`
rec_day=`echo "${recent_dirs}" | cut -d '-' -f1`
rec_month=`echo "${recent_dirs}" | cut -d '-' -f3`
# Go to your "home" directory for the directories.
cd /path/to/home/directory
for i in *; do
# Check to see if the element is a directory.
if [ -d "${i}" ]; then
echo "Processing directory - ${i} ... "
# Determine the date information for the directory.
cur_month=`cat "${i}" | cut -d '_' -f1`
cur_day=`cat "${i}" | cut -d '_' -f2`
# Keep all directories from the first of the month.
if [ "${first_day}" -eq "01" ]; then continue; fi
# Keep all directories from the current month and current year.
if [ "${cur_month}" -eq "${rec_month}" ]; then
if [ "${cur_year}" -eq "${rec_year}" ]; then continue; fi
fi
# Keep all directories from last month, but newer than today's DAY. I'm not
# a pro at bash arithmetic, so you might have to goof with the subshell
# math to get it to work quite right.
if [ "${cur_month} -eq $(( expr $rec_month - 1 )) ]; then
if [ "${cur_day}" -gt "${rec_day}" ]; then continue; fi
fi
# So at this point, I think we've dealt with everything, except the case where
# the current month is January, and so you're trying to save December's
# directories from last year. I think this handles it.
if [ "${rec_month}" -eq "01" ]; then
if [ "${cur_month} -eq "12" ]; then
if [ "${cur_year}" -eq $(( expr ${rec_year} - 1 )) ]; then continue; fi
fi
fi
# If we haven't stopped processing the directory by now, it's time
# remove our directory.
rm -fr "${i}"
else
echo "Skipping non-directory "${i}"...
do
exit 1
Something this won't do is deal with months with 31 days in it, so you may end up in many cases having saved 31 directories, instead of 30. I get the impression that you're trying to do a cleanup, and not a strict compliance routine, though...