ok this is what I came up with.
#!/bin/bash # Send everything to logs and screen. exec 1> >(logger -s -t $(basename $0)) 2>&1 # Variables and descriptions of their use. # Array of dates found in the filename of the backup files. arrDates=() # Number of full backup sets to keep. keep=4 # How many full backup sets have been found. found=0 # Bas path to the backup files, minus the last folder. base="/home/jbusch/" # Full path to the backup files, populated by the script. path="" # This script requires that the final folder name be passed as a paramter. # This is because it is designed to be ran independently for each subfolder. # ex: ./file_cleanup.sh Hartford # ex: ./file_cleanup.sh Seymour #check for the path to be passed if [ ! -z "$1" ] then # Create the full path to be checked based on the passed parameter. path=$base$1 else exit 127 fi printf "Executing cleanup of backup files located in $path.\n" # Loop through all of the files in the path and parse out an array of the file dates from the file names. # All backups are named `backup-0000001-YYYYMMDD-XXXX*`. cd $path for f in backup-* do # The date is from character 15 for 8 characters. arrDates=("${arrDates[@]}" "${f:15:8}") done cd ~ # Sort in reverse order and only show unique dates. arrDates=($(printf '%s\n' "${arrDates[@]}" | sort -ru)) # Loop through the array of dates and check for there to be 4 files for each date. for checkdate in "${arrDates[@]}" do count=$(find "$path"/backup-0000001-"$checkdate"-* -type f -printf '.' | wc -c) if [ $count -eq 4 ] && [ $found -lt $keep ] then found=$((found+1)) printf "Checking $checkdate, we found $count files. We are keeping this date, currently we have $found dates saved.\n" elif [ $count -gt 0 ] && [ ! $count -eq 4 ] then printf "Incorrect number of files '('$count')' found, removing invalid backup dated $checkdate.\n" rm $path/backup-*-$checkdate-* elif [ $count -gt 0 ] && [ $found -eq $keep ] then printf "We have already found $keep full sets of backup files. Removing backup files dated $checkdate.\n" rm $path/backup-*-$checkdate-* else printf "The date $checkdate returned $count files. This is an unhandled scenario, doing nothing.\n" fi doneoutput look like this
[jbusch@dt-jared FTPTest]$ ./file_cleanup.sh FTPTest <13>Jan 7 16:51:59 file_cleanup.sh: Checking 20200107, we found 4 files. We are keeping this date, currently we have 1 dates saved. <13>Jan 7 16:51:59 file_cleanup.sh: Checking 20200105, we found 4 files. We are keeping this date, currently we have 2 dates saved. <13>Jan 7 16:51:59 file_cleanup.sh: Checking 20200104, we found 4 files. We are keeping this date, currently we have 3 dates saved. <13>Jan 7 16:51:59 file_cleanup.sh: Checking 20200103, we found 4 files. We are keeping this date, currently we have 4 dates saved. <13>Jan 7 16:51:59 file_cleanup.sh: We have already found 4 full sets of backup files. Removing backup files dated 20200102. <13>Jan 7 16:51:59 file_cleanup.sh: We have already found 4 full sets of backup files. Removing backup files dated 20191230. <13>Jan 7 16:51:59 file_cleanup.sh: We have already found 4 full sets of backup files. Removing backup files dated 20191228.