-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleanup.sh
30 lines (26 loc) · 1.36 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# This script is designed to manage and clean up old directories and files within the ./downloads 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.
# 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.
# Traverse the download folder 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
# 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
done