This is one of the things I wish I knew earlier. There have been a lot of times, where I needed to constantly monitor the output of a command and all I would do is use the up(arrow) and enter key to constantly run the previous command. If you, too, have been in this situation, I hope you do what is required in a more optimized manner after reading this post.

The watch command

You can use this to execute a command repeatedly and monitor the output in full-screen mode. Suppose you want to constantly monitor the files inside a directory(e.g. /tmp/images), you may use the ls command with watch.

watch ls /tmp/images

By default, watch runs the command every 2 seconds, in case you need to change that value, you can pass the required value to the n parameter.

watch -n 3 ls /tmp/images

Highlighting the difference

While monitoring, it is generally a pain to monitor the difference manually. This is where the -d parameter comes to our rescue. You can use it to highlight the difference.

watch -d ls /tmp/images

A complex example

Now that we know what powers are bestowed to us by the watch command, let us try to monitor a more complicated scenario. Consider a situation where you would want to monitor the state of all python processes. The below command can help you do it.

watch -n 3 "ps aux | grep python"

The below screenshots try to show some part of the images when run on my machine.

Conclusion

In case, you are interested in hacking more into the watch command, you can use the -h flag to display all the options that you can use with it.

Seeing the complete list of options

watch -h

I hope you find this post useful and build some exciting stuff using the watch command.

Till we meet in another post, keep hacking and take care.