Simple rotate log script

Shell ScriptsThere are some situations where you have an automatic process that creates a log file. If the process is run on a periodic basis and you don’t add something like a timestamp to the log file name, the log ends up being overwritten and you lose the execution history.

This is a simple shell script that will rotate the log. It will keep the log for 6 days. The 7th’s day log is overwritten (you can easily modify the script to add more days).

Be sure to change the variable logdir to where your logs live.

The script takes the name of the log to be rotated as input. Assuming you name the script the way to call it would be


This is the script

#! /bin/sh


cd $logdir
test -f $logfile.5 && mv $logfile.5 $logfile.6
test -f $logfile.4 && mv $logfile.4 $logfile.5
test -f $logfile.3 && mv $logfile.3 $logfile.4
test -f $logfile.2 && mv $logfile.2 $logfile.3
test -f $logfile.1 && mv $logfile.1 $logfile.2
test -f $logfile && mv $logfile $logfile.1

Posted in Shell Scripts, Sysadmin, Unix and tagged . Bookmark the permalink. RSS feed for this post. Leave a trackback.

Swedish Greys - a WordPress theme from Nordic Themepark.