73
#!/bin/bash
# Define the directory where your logs are stored
LOG_DIR="/path/to/logs"
# Define the threshold date in epoch time (45 days ago)
THRESHOLD_DATE=$(date -d "45 days ago" +%s)
# Iterate over each log file in the directory
for LOG_FILE in "$LOG_DIR"/*.log; do
# Check if the file exists and is a regular file
if [ -f "$LOG_FILE" ]; then
# Get the last modification time of the log file in epoch time
LAST_MODIFIED=$(stat -c %Y "$LOG_FILE")
# Compare the last modification time with the threshold date
if [ "$LAST_MODIFIED" -lt "$THRESHOLD_DATE" ]; then
# If the file is older than 45 days, remove it
rm "$LOG_FILE"
echo "Removed old log: $LOG_FILE"
fi
fi
done
echo "Log cleanup completed."