Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a simple IRC bot in bash and I want prevent its log from growing to infinity. For example it could be shortened always when reaching set number of lines. How can I do that easily? For example by cutting the file with removing first line(s)?

If possible, I would like to avoid sed/awk this time :)

share|improve this question
Have you considered using logrotate? – jordanm Nov 1 '12 at 0:45

As jordanm said, logrotate is best. But if you do want to roll your own,

tail -n 50 logfile.txt > logfile.new
mv logfile.new logfile.txt

would save only the last 50 lines.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.