Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to remove the first N lines from a log that is being actively appended by an application?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

No, operating systems like Linux, and it's filesystems, don't make provision for removing data from the start of a file. In other words, the start point of storage for a file is fixed.

Removing lines from the start of a file is usually accomplished by writing the remaining data to a new file and deleting the old. If a program has the old file open for writing, that file's deletion is postponed until the application closes the file.

share|improve this answer
1  
But you should not do this while something is still has the file open and still appending to it, because it would write to the now deleted file, and you would lose those log messages. –  Tarnay Kálmán Mar 27 '12 at 11:37
add comment

I think this task can be achieved with sed

sed -i '1,10d' myfile

would remove the lines from 1st to the 10th line form the file.

I think everybody should at least have a look at this sed 1 liners.

share|improve this answer
    
Do you know what happens to the applications that are appending? –  Adam Matan Mar 27 '12 at 14:38
    
depends on the application. what are you using? –  l1zard Mar 27 '12 at 17:21
    
Let's assume a normal open file handler which appends lines and flushes every now and then. –  Adam Matan Mar 27 '12 at 19:01
    
then you can use it without -i and with a different output file instead. sed '1,10d' myfile > myfile.new. just spend a few hours in understanding how sed works. it's worth it. :) –  l1zard Mar 27 '12 at 19:06
1  
No, this should not work. sed -i creates a new file with the edited content and the old one is removed so you are not editing the active file: $ ls -i --- 6823554 testfile --- $ sed -i 's/test/final/' testfile --- $ ls -i --- 6823560 testfile ------ Please check how does sed -i work. Why does this wrong answer have so many upvotes? –  pabouk Dec 16 '13 at 9:57
show 2 more comments

No. A solution to this generic problem of log file growth is log rotation. This involves the regular (nightly or weekly, typically) moving of an existing log file to some other file name and starting fresh with an empty log file. After a period the old log files get thrown away.

See: http://www-uxsup.csx.cam.ac.uk/~jw35/courses/apache/html/x1670.htm

share|improve this answer
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.