This month with the year prepended is $(date +%Y%m). Last month is previous_month_1=$(date -d '1 month ago' +%Y%m) with GNU date (i.e. on non-embedded Linux or Cygwin), and the month before is previous_month_2=$(date -d '2 months ago' +%Y%m). on other implementations, they're given by
this_month=$(date +%Y%m)
case $this_month in
*01) previous_month_1=$((this_month-89)); previous_month_2=$((previous_month-1));;
*02) previous_month_1=$((this_month-1)); previous_month_2=$((previous_month-89));;
*) previous_month_1=$((this_month-1)); previous_month_2=$((previous_month-1));;
esac
Now, to match all existing files dated from the previous two months:
fetch "AA_XX_$previous_month_1"?? "AA_XX_$previous_month_2"??
(Replace fetch by the command you want to run on those files.) If a month may have no matching file, the pattern will be left unchanged:
filter_month () {
case $1 in *'??') return;; esac
fetch "$@"
}
filter_month "$previous_month_1"??
filter_month "$previous_month_2"??
If you want to fetch all files corresponding to the days of that month, you'll either need some GNU date magic (not described here) or some leap year logic.
filter_month () {
case $1 in
*0[13578]|*1[02]) n=31;;
*0[469]|*11) n=30;;
*0002) if [ $((${1%????} %4)) -eq 0 ]; then n=29; else n=28; fi;;
*02) if [ $((${1%??} %4)) -eq 0 ]; then n=29; else n=28; fi;;
esac
while [ $n -gt 0 ]; do
d=$n
if [ $d -lt 10 ]; then d=0$d; fi
fetch "AA_XX_$1$d"
fi
}
filter_month "$previous_month_1"
filter_month "$previous_month_2"
find (1).