Script to truncate wtmp files

Hi,

Does anyone have a script to truncate the wtmp file.

I want to move older entries in the wtmp to a new file and move it out of var/adm and shrink the size.

What OS are you on?

Linux uses logrotated to handle this, on AIX you would use fwtmp and if on Solaris you would most likely use logadm.

I am on AIX.
How to use fwtmp to rotate the logs?

Here you keep the last 500 and compress the rest and store in /scratch.

KEEP=500
DEST=/scratch

# Delete all but last $KEEP lines from wtmp
/usr/lib/acct/fwtmp < /var/adm/wtmp > $DEST/wtmp.out
tail -$KEEP $DEST/wtmp.out | /usr/lib/acct/fwtmp -ci > /var/adm/wtmp

# Keep everything except last $KEEP line in $DEST bzip2 date-stamped file
sed -e :a -e '$d;N;2,'$KEEP'ba' -e 'P;D'  $DEST/wtmp.out | bzip2> $DEST/wtmp_$(date +%Y%m%d).bz2
rm $DEST/wtmp.out

Note: if you don't have bzip2 installed, you could use compress instead

Thank you for the script.
let me test in one of my server.

---------- Post updated at 09:36 AM ---------- Previous update was at 08:51 AM ----------

It worked. Perfect script.
Thank you very much Chuber XL.