I like the other answers, but I have a simpler solution.
Also, the original question assumes that the last-of-month (date) file is always there. But we all know that you don't always have the snapshot from the last of the month.
I'm modifying the problem a bit and answering that;
- keep last file for each month that is there, not necessarily the 31st, 30th, 28th
- keep files that are 0-90 days old
- remove files older than 90 days, but not if they are the last for a month.
I don't care about the list of files in the example, because the approach is important. You can adjust the input if you understand the approach.
Given a random list of days:
function dates() { echo 2022-12-{06..12} 2022-{01,03,05}-{00..31} 2022-02-{00..28} 2022-{04,06}-{01..30} 2022-12-{01..06} 2022-10-{01..03}| tr ' ' \\n; }
In this list all days are present in months 1-6, only 12 days are listed in December and only 3 days in October.
To find the last days present in this list for each month, we will sort the list in ascending order and then remember every "oldest" date for each month. This gives the last of month.
$ dates | sort \
| awk -F- '{ lom[$1$2]=$1"-"$2"-"$3 } END { for (i in lom) { print lom[i]} }' \
| tee /tmp/lom
2022-01-31
2022-02-28
2022-03-31
2022-04-30
2022-05-31
2022-06-30
2022-10-03
2022-12-12
I don't care about the calendar dates in a real-life IT problem. I care about the files that are actually there. If the 12th is the last snapshot for December and there is no 31st, because the system was broken that day, then I want to keep the 12th.
So now we know what not to delete. The other part is older than 90 days:
dates | awk -v cutoff=$(date +%Y-%m-%d -d 'today -90 days') \
'{ if ($1 < cutoff) { print $1 } }' \
| grep -v -f /tmp/lom
This will print dates that are over 90 days old and exclude the last of moth entries. Short and sweet. Perfect.
As the guru pointed out, the whole thing above can be done in one line. wow!
dates | sort -r | awk -v cutoff=$(date +%F -d '-90 days') -F- '$0 < cutoff && seen[$1$2]++'
The generated list of dates to purge contains this:
2022-01-00 2022-03-11 2022-05-22 2022-04-05
2022-01-01 2022-03-12 2022-05-23 2022-04-06
2022-01-02 2022-03-13 2022-05-24 2022-04-07
2022-01-03 2022-03-14 2022-05-25 2022-04-08
2022-01-04 2022-03-15 2022-05-26 2022-04-09
2022-01-05 2022-03-16 2022-05-27 2022-04-10
2022-01-06 2022-03-17 2022-05-28 2022-04-11
2022-01-07 2022-03-18 2022-05-29 2022-04-12
2022-01-08 2022-03-19 2022-05-30 2022-04-13
2022-01-09 2022-03-20 2022-02-00 2022-04-14
2022-01-10 2022-03-21 2022-02-01 2022-04-15
2022-01-11 2022-03-22 2022-02-02 2022-04-16
2022-01-12 2022-03-23 2022-02-03 2022-04-17
2022-01-13 2022-03-24 2022-02-04 2022-04-18
2022-01-14 2022-03-25 2022-02-05 2022-04-19
2022-01-15 2022-03-26 2022-02-06 2022-04-20
2022-01-16 2022-03-27 2022-02-07 2022-04-21
2022-01-17 2022-03-28 2022-02-08 2022-04-22
2022-01-18 2022-03-29 2022-02-09 2022-04-23
2022-01-19 2022-03-30 2022-02-10 2022-04-24
2022-01-20 2022-05-00 2022-02-11 2022-04-25
2022-01-21 2022-05-01 2022-02-12 2022-04-26
2022-01-22 2022-05-02 2022-02-13 2022-04-27
2022-01-23 2022-05-03 2022-02-14 2022-04-28
2022-01-24 2022-05-04 2022-02-15 2022-04-29
2022-01-25 2022-05-05 2022-02-16 2022-06-00
2022-01-26 2022-05-06 2022-02-17 2022-06-01
2022-01-27 2022-05-07 2022-02-18 2022-06-02
2022-01-28 2022-05-08 2022-02-19 2022-06-03
2022-01-29 2022-05-09 2022-02-20 2022-06-04
2022-01-30 2022-05-10 2022-02-21 2022-06-05
2022-03-00 2022-05-11 2022-02-22 2022-06-06
2022-03-01 2022-05-12 2022-02-23 2022-06-07
2022-03-02 2022-05-13 2022-02-24 2022-06-08
2022-03-03 2022-05-14 2022-02-25 2022-06-09
2022-03-04 2022-05-15 2022-02-26 2022-06-10
2022-03-05 2022-05-16 2022-02-27 2022-06-11
2022-03-06 2022-05-17 2022-04-00 2022-06-12
2022-03-07 2022-05-18 2022-04-01 2022-06-13
2022-03-08 2022-05-19 2022-04-02 2022-06-14
2022-03-09 2022-05-20 2022-04-03 2022-06-15
2022-03-10 2022-05-21 2022-04-04 2022-06-16
access date
,change date
orbirth/created date
? Or are you working directly with the filenames? – Edgar Magallon Sep 15 '22 at 06:01ABC.txt.20220529 2022-05-30
orABC.txt.20220529
? – Edgar Magallon Sep 15 '22 at 06:03