Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce cleanup.sh #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -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