Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I check using Linux / Bash how many lines were added to selected log file during for example last 10 seconds ?

It's one time use, not on to run in the background. File doesn't contain any information about time.
I also don't need this arbitrary -> I can select a moment from when I would like to track the number of rows added, and do this for some time.

Thanks!

share|improve this question
1  
Does this log file actually contain time of log? –  Grumpy Oct 12 '12 at 11:49
1  
Do you intend to run this regularly? You could just wc -l, sleep, wc -l and then return the difference. –  Laykes Oct 12 '12 at 11:50
    
@Peter Thanks I have edited the question –  Wojtek Oct 12 '12 at 11:58
1  
Maybe timeout -k 10 tail -f filename | wc -l –  Zoredache Oct 12 '12 at 15:45
    
@Zoredache I alsmost managed to get it working, this is what I used: timeout 10 tail -f a.log | wc -l Unfortunately I get this on the output: Terminated, and not what I would expect which is number of rows –  Wojtek Oct 16 '12 at 15:34

4 Answers 4

Based on Zoredache's answer: it looks like timeout also kill pipe, but you still can forward the output to some temporary file and then count the lines in it.

timeout 2 tail -f logfile >/tmp/outp ; cat /tmp/outp | wc -l

or with watch

watch -n3 "bash -c 'timeout 2 tail -f logfile >/tmp/outp ; cat /tmp/outp | wc -l'"
rm /tmp/outp
share|improve this answer

You cannot do this unless you know you will need to do it in advance (which would allow you to copy the file or snapshot the filesystem at the right time). Even versioned filesystems (which are rare) don't log each character or line that changes as it changes. The best you could ever hope to achieve would be to say "The last change was this data and it happened between time X and time Y".

share|improve this answer

It's one time use, not on to run in the background. File doesn't contain any information about time.

Then it's a lot harder, no utility does this out of the box because if it did, it would imply that it knew about the files contents 10 seconds ago. But, there is nothing magical about "10 seconds" that would mean write data is kept/buffered for that long (unless there is some secondary logging about what it wrote to the log file, and when).

Your best bet is to either:

  • Run the commmand that Laykes gave or an equivalent like tail -s 10 -f FILE so it outputs the contents of the file in 10 second blocks.
  • Mark the log file itself: echo '-- MARKER --' >> FILE and check back for what you want.

But, either way, you'll need to roll your own solution.

share|improve this answer

You can set up a job that will log time and number of lines in the file. Alternate way would be to insert timestamp in the log file and check number of lines between timestamps.

share|improve this answer

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.