there is a completely different way to do this, with bash, using the ed program. the following script will retain only the last 5000 lines of all files sitting in the specified directory. this can easily be modified to loop over several directories, change the number of lines, etc.
#!/bin/bashLOGDIR=/opt/logSAVELINES=5000dirs="$LOGDIR"for dir in $dirs ; do files=${dir}/* for f in $files ; do echo -e "1,-${SAVELINES}d\nwq" | ed $f 1>/dev/null 2>&1 donedone