Skip to content

Instantly share code, notes, and snippets.

@allex
Created May 4, 2015 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allex/c487d6d8b2b52219a87b to your computer and use it in GitHub Desktop.
Save allex/c487d6d8b2b52219a87b to your computer and use it in GitHub Desktop.
#!/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