Unix Tip #4422- April 20, 2023


SEARCH : Home : Help : Today's Tip

TRIMMING THE LOG



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.

NOTE: All tips provided are USE AT YOUR OWN RISK. Tips are submitted by various unix admins around the globe. UGU suggest you read and test each tip in a non-volitile environment before placing into production.


LAST 5 TIPS
4421 - FOLDING IN LINUX
4420 - THE NEXT UID
4419 - TRANSFERRING MULTIBYTE FILES
4418 - LOGGING MAKES
4417 - ALIAS YOUR CONNECTIVITY


I want to SUBSCRIBE and get a UGU Tip everyday.
I want to UNSUBSCRIBE and NOT get a UGU Tip everyday.

If you have a UNIX TIP let us know, we just may use it:
(All tips become the property of the Unix Guru Universe)
Email Address:

Yes, email me a Hot Unix Tip everday.

Enter Hot Unix Tip (optional):

Yes, I will support this tip

Captcha (not case sensitive):


Please enter the above letters:


HOME | Flavors | Admin | Network | Security | S/W | Help | Events | Vendors | Careers | Internet
About | Add Link | Feedback | Search

Copyright © 1994-2005 Unix Guru Universe