-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear-downloads.sh
executable file
·56 lines (47 loc) · 1.66 KB
/
clear-downloads.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Remove files in Downloads folder older than 2 weeks
# Make a cron job with following directions:
# - Replace `leesamuel423` with your username
# - `crontab -e` and add the below lines
# # Task: Run clearing items older than 2 weeks from Downloads folder
# # Schedule: Weekly on Sunday at 3 AM
# # 0 3 * * 0 ~/.scripts/cleanup-downloads.sh # Adjust this section accordingly
DOWNLOADS_DIR="/Users/samuellee/Downloads"
LOG_DIR="/Users/samuellee"
LOG_FILE="$LOG_DIR/cron_logs.txt"
mkdir -p "$LOG_DIR"
exec 1>>"$LOG_FILE" 2>&1
echo "=== Starting cleanup at $(date) ==="
# Check if the downloads directory exists
if [ ! -d "$DOWNLOADS_DIR" ]; then
echo "Error: Downloads directory not found at $DOWNLOADS_DIR"
exit 1
fi
echo "Cleaning up Downloads folder..."
# Remove files older than 14 days, including installers
echo "Removing old files..."
if find "$DOWNLOADS_DIR" \( \
-type f -o \
-name "*.dmg" -o \
-name "*.pkg" -o \
-name "*.exe" -o \
-name "*.msi" -o \
-name "*.deb" -o \
-name "*.rpm" \
\) -mtime +14 -print -delete; then
echo "File cleanup completed successfully"
else
echo "Error during file cleanup"
exit 1
fi
# Remove non-empty directories older than 14 days
echo "Removing old non-empty directories..."
find "$DOWNLOADS_DIR" -type d -mtime +14 -not -empty -not -path "$DOWNLOADS_DIR" -print0 | while IFS= read -r -d '' dir; do
rm -rf "$dir"
echo "Removed directory: $dir"
done
# Remove all empty directories regardless of age
echo "Removing empty directories..."
find "$DOWNLOADS_DIR" -type d -empty -not -path "$DOWNLOADS_DIR" -print -delete
echo "=== Cleanup completed at $(date) ==="
echo "----------------------------------------"