I am trying to write a command that will keep only the last 2 backups and delete the remaining directories.
directory path = /apps/
Below are the directories and here I would like to keep only
source
source_2020-01-13
source_2019-12-09
directories and delete the old ones. This script will run across 100+ servers using cron.
Here problem is, in find
command I wont be able to add mtime
to delete old directories - why because backup directories timestamp will not be same on all servers. Basically the command/script should keep the last two backup directories that is with source_YYYY_MM_DD name and delete the remaining ones.
Can someone please assist?
Note : Source_YYYY_MM_DD are the backup directories.
drwx------ 11 root root 4096 Jul 22 2019 source_2019-07-22
drwx------ 11 root root 4096 Jul 29 2019 source_2019-07-29
drwx------ 11 root root 4096 Aug 5 2019 source_2019-08-05
drwx------ 11 root root 4096 Aug 14 2019 source_2019-08-14
drwx------ 11 root root 4096 Oct 14 2019 source_2019-10-14
drwx------ 11 root root 4096 Dec 9 03:59 source_2019-12-09
drwx------ 11 root root 4096 Jan 13 05:20 source_2020-01-13
drwxr-x--- 12 root root 4096 Apr 21 09:11 source