diff --git a/cleanup.sh b/cleanup.sh index 95d3272..67e5b31 100644 --- a/cleanup.sh +++ b/cleanup.sh @@ -1,30 +1,28 @@ #!/bin/bash -# This script is designed to manage and clean up old directories and files within the ./downloads folder. +# This script is designed to manage and clean up old directories and files within the ./downloads folder and ./uploads folder. # Its primary purpose is to delete directories and their contents that are older than 1 week (168 hours) to free up disk space -# and maintain a clean file system. This script will iterate through all the files in the ./downloads folder -# and delete all sub-folders and files if the folder was created more than 1 week ago. If there are files in this folder, -# the files will also be deleted one by one. +# and maintain a clean file system. This script will iterate through all the folders in the /data/downloads folder +# and delete all sub-folders and files if the folder was created more than 1 week ago for ./downloads folder +# or 4 days ago for ./uploads folder. # Define path to the downloads directory -download_folder="./downloads/" -# cutoffMins = 1 week ago = 168 hours ago (in seconds) -cutoffMins=$((169 * 60)) # add 1 hour to handle edge case, which is job finished at 1:00 am. +download_folder="/data/downloads/" +downloadCutoffDays=8 -# Traverse the download folder and remove directories older than cutoff time +upload_folder="/data/uploads/" +uploadCutoffDays=4 + +# Traverse the download and remove directories older than cutoff time # -mindepth 1: Excludes the top-level directory, includes only subdirectories -find "$download_folder" -mindepth 1 -type d -mmin +$((cutoffMins)) | while read -r dirpath; do +find "$download_folder" -mindepth 1 -type d -ctime +${downloadCutoffDays} -mtime +${downloadCutoffDays} | while read -r dirpath; do + # echo "$dirpath" + rm -rfv "$dirpath" +done + +# Traverse the upload folder and remove directories older than cutoff time +find "$upload_folder" -mindepth 1 -type d -ctime +${uploadCutoffDays} -mtime +${uploadCutoffDays} | while read -r dirpath; do # echo "$dirpath" - if [ -d "$dirpath" ]; then - timestamp=$(date '+%Y-%m-%d %H:%M:%S') - rm -rf "$dirpath" - # Captures the success of the rm command - result=$? - if [ $result -eq 0 ]; then - echo "Removed old directory: $dirpath - $timestamp" - else - echo "Error removing directory: $dirpath - $timestamp" - fi - fi + rm -rfv "$dirpath" done