linewatch - an alternative to linux's watch

I often use the linux watch command to monitor the status of certain commands. When I'm copying lots of files say, I'd watch the files in the target directory to see what files have already been copied across with the following command:

watch ls -l

The watch program clears the screen and displays the output of "ls -l" every 2 seconds.

Sometimes I'll want to monitor a command that only outputs a single line. If I wanted to see the total number of files in a directory rather than the files themselves I could use the command "ls -l | wc -l". The fact that watch clears the whole screen can be a little annoying here though, because the command is only outputting a single line. That is why I came up with the following small bash script, linewatch.

Linewatch repeatedly calls any arguments passed to it every 2 seconds (in the same way watch does), but only clears a single line rather than the whole screen. Here is the code:


while true 
    eval "$command"
    sleep 2
    echo -n -e "$clearline"

And here is an example of how to call it:

$ ./linewatch "ls -l | wc -l"

The number of files in the current directory (24 in the example) will keep update every 2 seconds. Just hit Ctrl-C when you want to quit,

Posted on 18 Jul 2009
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.