[nylug-talk] logrotate question

C Thala cthala at gmail.com
Sat Feb 2 16:47:31 EST 2008


I have this problem that I've encountered a couple of times before, that I
think should be solved in an elegant way, but I can't seem to find the
solution.

I generate files every night (or hour, or week, etc.). These files are of the
form "name.$datestamp", e.g.:

    foo.2008-02-02T16:16:05-0500.gz
    foo.2008-02-02T16:16:06-0500.gz
    foo.2008-02-02T16:16:07-0500.gz
    foo.2008-02-02T16:16:08-0500.gz
    foo.2008-02-02T16:16:09-0500.gz
    foo.2008-02-02T16:16:10-0500.gz
    foo.2008-02-02T16:16:21-0500.gz


I would like to keep the last N files. Let's say 5.

Now in the past, I've written a shell script of the form,

    #!/bin/sh
    foocount=`ls foo*gz|wc -l`
    if [ "$foocount" -le 5 ]; then
        exit 0; # nothing needs to be deleted
    fi
    filestodelete=`expr $foocount - 5`
    rm `ls foo*gz|head -n $filestodelete`

But this is ugly.

I'm looking for an elegant solution.

So I thought logrotate was the way to go, but I can't get it to just delete any
files over N number of files.

Can someone help?


More information about the nylug-talk mailing list