The following code does not preserve the last file of each month.
#! /bin/bash
cmp_timestamp=$(date --date="60 days ago" +%Y%m%d)
while read filename; do
[[ $filename =~ _(20[0-9][0-9][01][0-9][0123][0-9])\. ]]
timestamp=${BASH_REMATCH[1]}
printf "%-40s : %s\n" "$filename" "${timestamp}"
if [ "$timestamp" -lt "$cmp_timestamp" ]; then
echo " delete this file"
: rm "$filename"
else
echo " DO NOT delete this file"
fi
echo
done <file
From man bash
:
BASH_REMATCH
An array variable whose members are assigned by the =~ binary operator to
the [[ conditional command. The element with index 0 is the portion of the
string matching the entire regular expression. The element with index n
is the portion of the string matching the nth parenthesized subexpression.
This variable is read-only.