You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk '{print $NF}')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".