Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/sh
# Auto cleanup expried log files.
# Author: Allex Wang (allex.wxn@gmail.com)
# GistID: c487d6d8b2b52219a87b
# GistURL: https://gist.github.com/c487d6d8b2b52219a87b
log_dir=$1 # log files directory
expired_days=7 # log file expried days
function delete_logs() {
# get system unix datestamps
local current_date=`date +%s`
for f in `find $1 -name "*.log"`
do
local modify_date=$(stat -c %Y $f)
# calculate the modified date diffs
local exist_time=$(($(($current_date - $modify_date))/86400))
if [ $exist_time -gt $expired_days ]; then
echo "Delete log file: $f, EXIST_TIME: ${exist_time}"
rm -f $f
fi
done
}
if [ -n "$log_dir" ]; then
delete_logs $log_dir
else
echo "logs dir not valid!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment