Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
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 9:22
    
@symcbean, yes, very good point! I suppose that could be handled with fuser. –  Dimitre Radoulov Oct 28 '11 at 9:24
add comment

Your Answer

 
discard

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.