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
Thanks in advance.
Have you heard about logrotate? I think it's not good to erase logs.
Writing to the file you're reading is not a good idea. My solution is also not good as you may lost log between tail and mv command.
But anyway
is a way to loop over files of a directory.
But why don't you use logrotate ??