1

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?

cwd
  • 45,389

1 Answers1

2

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
  • 3
    awesome. i should probably just use logrotate though. not sure what i was thinking. – cwd Oct 27 '11 at 19:50
  • 3
    Yes, you should definitely use logrotate. – Dimitre Radoulov Oct 27 '11 at 19:52
  • 1
    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 09:22
  • @symcbean, yes, very good point! I suppose that could be handled with fuser. – Dimitre Radoulov Oct 28 '11 at 09:24