Short Bytes:Have you ever needed to run a command every couple minutes to check on something? Say you need to watch a RAID rebuild or watch a log in real time, but need to search or filter it first. That takes a lot of specialized tools, one for each task really. But using watch command this can be achieved easily.

Whatwatchdoes is it runs the command in a loop, but clears the terminal before running it each subsequent time, and additionally, displays the interval, command, and date/time as the first line. The default interval is two seconds, but this can be manually set using the-nflag with a lower limit of one-tenth of a second.

watch-command-in-linux

Here, below, we run thefree(a memory usage reporting tool) command every five seconds.

As you can see, we were able to pass in the-m(display in megabytes) flag tofreewithout confusingwatch. This is because all arguments after the first argument, which is a non-option, are passed to the executed command. This gives you some freedom to pass commands without the need of quotes, though, in the cases wherepiping and redirectionare used, quotation marks will be required otherwise the output ofwatchwill be what’s piped.

There are many options that can be passed towatch, like-tto remove the header information, or-dto highlight the differences between each interval. Below is the full list according to the documentation.

With these options, it’s easy to see how we can combinewatchand a little bit of scripting with other tools (or sysadmin-fu as some like to call it) to create complex monitoring tools that are custom tailored to our specific needs.

Show us how youwatchin the comments below.

Also Read:Linux Lexicon — Input And Output With Pipes And Redirection In Linux