-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteOldVCSLogFilesScript.sh
More file actions
29 lines (25 loc) · 1.01 KB
/
DeleteOldVCSLogFilesScript.sh
File metadata and controls
29 lines (25 loc) · 1.01 KB
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
LOG_DIR="/home/gpm/FileClearLogs"
TARGET_DIR="/home/gpm/GPM_AGV_LOG"
Days=90
mkdir -p $LOG_DIR
LOG_FILE="$LOG_DIR/cleanup_$(date +'%Y-%m-%d').log"
sudo chmod -R 777 $TARGET_DIR
# Log the start of the clean
echo "Cleanup Files Older than $Days ago started at $(date)" >> "$LOG_FILE"
#delete file which older than 90 days
find "$TARGET_DIR" -type f -mtime +"$Days" -exec rm -f {} \; -exec echo "Deleted: {}" >> "$LOG_FILE" \;
#delete empty directory recursively until no empty directories remain
while true; do
# Try to remove empty directories and count how many were removed
empty_dirs=$(find "$TARGET_DIR" -type d -empty)
if [ -z "$empty_dirs" ]; then
break
fi
echo "$empty_dirs" | while read dir; do
rmdir "$dir" 2>/dev/null && echo "Removed empty directory: $dir" >> "$LOG_FILE"
done
done
# Log the completion of the cleanup
echo "Cleanup Files Older than $Days ago completed at $(date)" >> "$LOG_FILE"
echo "Deleted files older than $Days days ago from $TARGET_DIR COMPLETED."