From 54e093824e21aa740a46c970cbc0a187a98e2b11 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 13 Mar 2021 09:53:13 -0500 Subject: [PATCH] Add recursive subdir logic to "clear backups" script --- backups/clear_backups.sh | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/backups/clear_backups.sh b/backups/clear_backups.sh index ada95f2..47620f7 100755 --- a/backups/clear_backups.sh +++ b/backups/clear_backups.sh @@ -15,6 +15,8 @@ # Optional Flags: # * Force - Force script to execute without confirmation of provided args. # * Accepted Flags: f / force + # * Recursive - Also execute script on all subfolders within original directory. + # * Accepted Flags: r / recursive # # # https://git.brandon-rodriguez.com/scripts/bash/server_management @@ -23,7 +25,7 @@ # Define possible flag values for script. -possible_flags=("-f" "-force") +possible_flags=("-f" "-force" "-r" "--recursive") # Import utils script. @@ -52,7 +54,15 @@ function main () { confirm_args fi - clear_backups + # Check if should handle recursively. + if [[ ${flags[@]} =~ "r" || ${flags[@]} =~ "recursive" ]] + then + # "Recursive" flag provided. Execute on base directory and all subdirectories. + recurse_clear_backups "${directory}" + else + # No "recursive" flag provided. Only execute on base directory. + clear_backups + fi } @@ -222,4 +232,28 @@ function clear_backups () { } +### + # Logic to recursively clear backups from folder and all subfolders. + # Only clears files that match expected naming format. + ## +function recurse_clear_backups () { + orig_directory=${1} + + # Execute on current directory. + directory="${orig_directory}" + clear_backups + + # Execute on all subdirectories. + for dir in ${orig_directory}/* + do + # Check if is directory. + if [[ -d "${dir}" ]] + then + # Call recursive logic on directory. + recurse_clear_backups "${dir}" + fi + done +} + + main -- GitLab