Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do some simple computation of the number of lines per minute added to a log file.

I want also to store the count for each second.

What I need is the output of the following command as a list which will be updated every second:

watch -n1 'wc -l my.log'

How to output the 'update' of the 'watch' command as a list?

share|improve this question

You can use the -t switch to watch which causes it not to print header. However, that will still clear the screen so you might be better off with a simple shell loop:

while sleep 1; do
    wc -l my.log

One of the advantages is, that you can easily add other commands (e.g. date) and/or pipe the output through sed to reformat it. By the way, if you swap sleep 1 with wc in the loop, it will automatically terminate on errors.

share|improve this answer
However note that it will not do it exactly every second (with zsh or ksh93, you could adjust the sleeping time so as to account for the drift incurred by running the commands in the loop, though) – Stéphane Chazelas Nov 19 '12 at 11:51
@StephaneChazelas neither will it with wait - just try watch -n 1 "sleep 5". – peterph Nov 19 '12 at 12:26
Indeed (I've checked both procps and busybox implementations). I thought that was the only thing watch was useful for, but it doesn't give you even that so your solution is as good as a watch based one, but neither answers the "rate at which a log file grows" question with great accuracy. – Stéphane Chazelas Nov 19 '12 at 13:22
Well, if lines per minute are the ultimate goal, then sampling it once every 10 seconds is more than enough imho - so the overhead is not that terrible (unless the file grows really big, of course). And actually from within the loop, one can print timing information (even both before and after the command finishes if necessary), and then the accuracy can get better (orders of magnitude) even for bigger files. – peterph Nov 19 '12 at 13:28
@peterph watch has a -p option that will do that right, if at all possible (obviously, you can't do a command that takes 5 seconds every 1 second, if you're not allowed multiple simultaneous). I know, I wrote it :-P – derobert Jun 19 '14 at 17:46

How about

tail -f file.log | pv -rl > /dev/null
share|improve this answer

An old question, but I just found a very easy answer:

watch -n1 'wc -l my.log | tee -a statistics.log'

This will execute your wc each second, add its output to the statistics.log file, and also show it on the screen.
So, you'll end up with a file filled with numbers, representing the successive number of lines of my.log.

share|improve this answer

Try the following:

watch -n1 'wc -l my.log >> statistics.log'
share|improve this answer

You could create a script that does it for you. I called mine keep (like, keep doing it) and put it on the bin path.

This is my script:

echo "Repeating command $* every second"
while sleep 1; do
share|improve this answer
You should use "$@" to execute the command (arguments) instead of unquoted $*. This way you'll have the shell keep quoted arguments, etc. as the user would expect them. FTFY. – roaima Mar 3 at 22:56

Your Answer


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.