I use Linux at work and for my development server at home. For quite a while at work I've been keeping logs of certain functions to make sure everything is running properly, primarily with processes that are kicked off by automatically in the middle of the night.
After a while, I like to shift the logs so that they aren't getting too big. As part of the clean up process, I would rename any older files for the same process by appending numbers 1 through 5 to the end of the file name. This was really just an arbitrary choice, but it was inspired by the Yii application.log files that were stored in the same location. At first it was no big deal... Still isn't really, but I thought it would be neat to write a simple script that could batch rename some files bases on my needs. For now, this script is run manually, although it might eventually get added to a cron job. Posting it here is two-fold; so I have a copy of it stored online for me to find easily, and to share something that might be helpful to others.
#!/bin/bash
# Programmer: Nathaniel Dragon - Oct 2012
# This script is for renaming logs. One parameter is needed to tell the script which group of logs to rename.
# For example, to rename App.log, App.log.1, and App.log.2
# simply run this script with App.log as the first parameter, like so:
# ./shiftLogs App.log
args=$# # Number of args passed.
#echo $args
if [ "$args" -ne "1" ]
then
echo "Missing required argument: a log name is required."
exit $E_MISSING_POS_PARAM
fi
# Warn user before continuing.
echo "Renaming these files: "
#list="$(ls -r $1*)"
list=`ls -r $1*`
echo $list
echo "Are you sure you want to continue? (y|n)"
read -e confirm
if [ "$confirm" != "y" ]
then
echo "Process aborted."
exit 0
fi
# loop through the files to rename them
for x in $list; do
chars=${#x} # string length
ending=${x:$chars-1}
#echo $ending
if (($ending > 0 && $ending < 5))
then
echo "Shifting $x"
(( ending += 1 ))
sudo mv "$x" "$1.$ending"
elif (($ending == 5))
then
echo "Removing $x"
sudo rm "$x"
else
echo "Archiving $x"
sudo mv "$x" "$1.1"
fi
done
By the way, if you are interested in learning more about Bash scripting, I recommend
http://tldp.org/LDP/abs/html/... Or... If you see something that I've done wrong in this script, please post a comment and let me know how to fix it.