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 have a log file that gets big fast. I tried using tail with this syntax but it didn't work.

tail logfile.log -n 100000 > logfile.log

The output file is 0 bytes and blank. What am I doing wrong? Will I need to use an intermediary file?

share|improve this question
See also Is there a way to modify a file in-place? – Gilles Oct 27 '11 at 22:21
up vote 2 down vote accepted

You could use something like this, but it's not really recommended:

{ rm logfile.log && tail -n 100000 > logfile.log ;} < logfile.log

This is better:

tail -n 100000 logfile.log > _tmp_ &&
  mv -- _tmp_ logfile.log
share|improve this answer
awesome. i should probably just use logrotate though. not sure what i was thinking. – cwd Oct 27 '11 at 19:50
Yes, you should definitely use logrotate. – Dimitre Radoulov Oct 27 '11 at 19:52
the second method is better - but misses a very important step. If there is still a file handle open on logfile the footprint will exist on the disk until it is closed (and the application will continue to write to the old footprint - not the new file). You need to tell the application to reopen the file. – symcbean Oct 28 '11 at 9:22
@symcbean, yes, very good point! I suppose that could be handled with fuser. – Dimitre Radoulov Oct 28 '11 at 9:24

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.