Linux Tip: Trimming the logs

April 25, 2013
By

tux

Various unix processes can produce fast growing logs that sometimes need to be trimmed instead of deleted, for reference or troubleshooting. And you likely have no desire to edit the files. Here is a handy ksh
script that will quickly trim the log so it keeps recent information and lets you keep as many lines as you think you might need. I call it trimlog:

#! /bin/sh
# trimlog
filesize=`cat $1|wc -l`
trim=`expr $filesize – $2`
if [ $trim -gt 0 ]
then
sed “1,$trim d” $1 > /tmp/$1
mv /tmp/$1 $1
echo $1 trimmed by $trim lines
fi

Use it by feeding in the name of the log you want to trim followed by the number of lines you want to keep:

# trimlog oracle_listener.log 10000
Of course you can’t use it in the /tmp directory. There are probably newer and more efficient ways to do this, but this works.

Tags: , , , , ,

Search

Advertisement

Affiliates

Featured Video

Translator

Links