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.


  • 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
  • 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 – wlk Oct 16 '12 at 15:34

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.


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.


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".


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.