How To Use The Watch Command With Examples

Ever have a time where you needed to continuously check the progress of something, for example the progress of your array rebuilding after replacing a hard drive? Or waiting for the status of something to change?

Well instead of running the same commands over and over again, there is an easier way. You can simply use the watch command to periodically run the specified command or script for you at whatever interval you would like. Below is the usage as-well as available options.

Watch Command Usage

watch [options] “command”

-bBeep if command has a non-zero exit (if command fails)
-cInterpret ANSI color and style sequences
-dHighlight changes between updates
-eExit if command has a non-zero exit
-gExit when output from command changes
-nInterval <secs> seconds to wait between updates
-pAttempt run command in precise intervals
-tTurn off header (shows interval and command)
-xPass command to exec instead of “sh -c”
-hDisplay this help and exit
-vOutput version information and exit

Watch Command Examples

Here are a few examples of using the watch command. I will be using it to watch the output of “date” for the sake of this guide, since everyone running Linux should have the date command available. To terminate watch and whatever command or script it is currently running, you can simply use CTRL+C.

Refresh every 5 seconds:

watch -n5 "date"

Refresh every 5 seconds and highlight any output changes:

watch -n5 -d "date"

Refresh every 5 seconds and exit if output changes (you will not see the output if you choose this option):

watch -n5 -g "date"

I hope you liked this post about the watch command, please don’t forget to like, comment or share this post. Thanks!


Leave a Reply

Your email address will not be published. Required fields are marked *