I am looking to create a cron job that opens a directory loops through all the logs i have created and deletes all lines but keep the last 500 for example.
I was thinking of something along the lines of
tail -n 500 filename > filename
Would this work?
I also not sure how to loop through a directory in bash.
If log file to be truncated is currently open by some services, then using mv as in previous answers will disrupt those services. This can be easily overcome by using cat instead:
tail -n 1000 myfile.log > myfile.tmp
cat myfile.tmp > myfile.log
Think about using logrotate.
It will not do what you want (delete all lines but the last 500), but it can take care of logfiles which are bigger than a certain size (normally by comressing the old ones and deleting them at some point). Should be widely available.
In my opinion the easiest and fastest way is using a variable:
LASTDATA=$(tail -n 500 filename)
echo "${LASTDATA}" > filename
DIR=/path/to/my/dir # log directory
TMP=/tmp/tmp.log # temporary file
for f in `find ${DIR} -type f -depth 1 -name \*.log` ; do
tail -n 500 $f > /tmp/tmp.log
mv /tmp/tmp.log $f
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With