Shell Script to Delete old log files and Send mail



In post I am sharing sample shell script to delete old log file from a location and send mail


Script:

#!/bin/bash

# Directory path where log files are stored
log_directory="/u01/oracle/app/diag/test"

# Maximum age of log files (in days)
max_age=10

# Email configuration
email_recipient="support@funoracleapps.com"
email_subject="Deleted Log Files Report from TesT"

# Delete old log files and collect the deleted file names one by one
deleted_files=$(find "$log_directory" -type f -name "*.log" -mtime +"$max_age" -exec rm {} \; -print)

# Generate email body
if [ -n "$deleted_files" ]; then
    email_body="Deleted log files:$'\n'$deleted_files"
else
    email_body="No log files were deleted."
fi

# Send email to recipient
echo "$email_body" | mail -s "$email_subject" "$email_recipient"







If you like please follow and comment