[SunHELP] Shell Script

Kovalev, Ivan sunhelp at sunhelp.org
Mon Mar 26 13:45:41 CST 2001


To truncate the file you can use cat /dev/null >file.
To keep last n lines, use tail -n file.
To do one file at a time use find:
find <dir where to start from> -name "*.log" -exec cat /dev/null > {} \;&
this sample will truncate all *.log files

For overwriting original file with output from tail you would put overwrite
script (sorry, forgot where did I pick it up from) in place of command for
find.

#!/bin/ksh
# set -o xtrace
     #overwrite: overwrite file with output of command
     #note: must use redirection of input from file in command if required



     case $# in
         0|1) echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
     esac

     file=$1; shift
     new=/tmp/overwr1.$$; old=/tmp/overwr2.$$
     trap 'rm -f $new $old; exit 1' 1 2 15

     if "$@" > $new
     then
         cp $file $old
         trap '' 1 2 15
         cp $new $file
     else
         echo "overwrite: $1 failed, $file unchanged" 1>&2
         rm -f $new $old
         exit 1
     fi
     rm -f $new $old

-----Original Message-----
From: Balaji N [mailto:balaji_iii at hotmail.com]
Sent: Monday, March 26, 2001 9:18 AM
To: sunhelp at sunhelp.org
Subject: [SunHELP] Shell Script


Hi everyone

I have a directory which contains *.log files and txt files. I need a
shell  script which  can clear all lines in *.log files and keeping some
latest 100 lines . I can put the script as cron job, so that it can
run in weekly basis. can anyone have?

Thanks in advance
Balaji

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

_______________________________________________
SunHELP maillist  -  SunHELP at sunhelp.org
http://www.sunhelp.org/mailman/listinfo/sunhelp



More information about the SunHELP mailing list