[BUGS] scripting: moving/renaming log files

Christopher Vance cjsvance at gmail.com
Sun Nov 11 13:40:11 EST 2007


On 11/11/07, jonathan michaels <jlm at caamora.com.au> wrote:
> appreciate some pointers some suggestions, the rename to current
> date is easy bit (even i can do that .. grin). all the stuff i
> have looked at so far suggests digging into system programming.

I use the following script out of cron to rename files in place

# save log files by date/time
for f in /var/log/*.[0-9].gz /var/log/*.[0-9][0-9].gz
do
    d=$(perl -e 'use POSIX;
                @s=stat($ARGV[0]);
                print strftime("%Y%m%d%H%M\n",gmtime($s[9]));' $f)
    n=$(echo $f | sed -e 's/\.[^.]*$//;s/\.[^.]*$//;s/$/.'"$d"'.gz/')
    while test -f $f
    do
        mv -i $f $n
        n=$(echo $n | sed -e 's/\.gz$/p'"$$"'.gz/')
    done
done

I actually then occasionally use mv -i manually to move
/var/log/*.2007* into the relevant permanent location.

Depending on your shell, you may need to replace $( ) with backticks.
(I'm using OpenBSD's [k]sh, but can't say whether FreeBSD's sh can
handle it right.  I do know Solaris sh botches this badly.)

Because my system runs newsyslog hourly, and the rename happens only
daily, I need to make sure that syslog.conf keeps enough generations
to handle frequent rollovers.

-- 
Christopher Vance


More information about the BUGS mailing list