Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash edit file and keep last 500 lines

Tags:

bash

logging

tail

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.

like image 301
Lizard Avatar asked May 21 '10 09:05

Lizard


Video Answer


4 Answers

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
like image 72
user2553977 Avatar answered Oct 12 '22 13:10

user2553977


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.

like image 21
tanascius Avatar answered Oct 12 '22 14:10

tanascius


In my opinion the easiest and fastest way is using a variable:

LASTDATA=$(tail -n 500 filename)
echo "${LASTDATA}" > filename
like image 29
TheFax Avatar answered Oct 12 '22 12:10

TheFax


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
like image 44
Paul R Avatar answered Oct 12 '22 13:10

Paul R