Skip to content

Instantly share code, notes, and snippets.

@mqu
Created October 23, 2018 08:02
Show Gist options
  • Save mqu/b992abe6a9b9ca76614d74d6d219a9d1 to your computer and use it in GitHub Desktop.
Save mqu/b992abe6a9b9ca76614d74d6d219a9d1 to your computer and use it in GitHub Desktop.
rotating log file with bash script
#!/bin/bash
# rotate log files with bash script.
function _logrotate {
file=$1
MAXLOG=10
# return if $file does not exists
[ ! -f $file ] && return 0
# process file rotation
for i in `seq $((MAXLOG-1)) -1 1`; do
[ -f "${file}.${i}" ] && mv -fv "${file}."{$i,$((i+1))}
done
mv $file $file.1
return 0
}
# sample usage for demo
file=log/test.log
_logrotate $file
date >> $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment