Linux watch Command Tutorial for Beginners (5 Examples)
Sometimes, while working on the Linux command line, you might want to execute a command repeatedly so as to track any change in output. Well, you’ll be happy to know there exists a command-line utility that lets you do this. The tool in question is Watch, and in this tutorial, we will discuss some of its basic features using some easy to understand examples.
But before we do that, it’s worth mentioning that all examples used in this article have been tested on Ubuntu 16.04 LTS.eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-3′,’ezslot_2′,121,’0′,’0′]));
Linux watch command
As already mentioned, the watch utility lets you execute a command repeatedly. Following is the syntax of this command:
watch [options] command
And here’s how the tool’s man page describes it:
watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output change
over time. By default, the program is run every 2 seconds. By
default, watch will run until interrupted.
Following are some Q&A-styled examples that should give you a good idea on how the ‘watch’ command works.
Q1. How watch command works?
The basic usage of the watch command is very easy – all you have to do is to run the tool with a command name as input. For example:
The above command produced the following output on my system:
The first line of the output tells the output of the command ‘date’ is being refreshed after every 2 seconds (that’s the default time interval), followed by the current date/time of the system. The second line is the actual output of the ‘date’ command that’s getting refreshed after every couple of seconds.
Q2. How to make watch highlight difference between updates?
As already explained in the previous section, the output of the command being tracked is regularly updated (after 2 seconds by default). In case you want, you can also make ‘watch’ highlight the part of output that’s changing between updates.eval(ez_write_tag([[580,400],’howtoforge_com-medrectangle-4′,’ezslot_1′,108,’0′,’0′]));
This you can do using the -d command line option. For example:
watch -d date
And here’s how the watch command highlights the changes now:
Q3. How to customize update interval?
As already mentioned, the default update interval is 2 seconds. However, if you want, you can change this value by using the -n command line option, which requires you to pass the new interval value as its input.
watch -n 5 date
This command will make sure the output gets updated after every 5 seconds. Please note that intervals less that 0.1 seconds aren’t allowed.
Note: To make sure the interval gap is honored very precisely, use the -p command line option. Here’s how the man page describes this option:
Make watch attempt to run command every interval seconds. Try it
with ntptime and notice how the fractional seconds stays
nearly) the same, as opposed to normal mode where they continu?
Q4. How to turn off the header in output?
If you want, you can also ask ‘watch’ to turn off the header showing the interval, command, and current
time at the top of the display, as well as the following blank line. This can be done using the -t command line option.
watch -t date
Here’s the output produced by the command above:
So you can see the header was completely stripped off.
Q5. How to make watch exit when the output changes?
If you want the ‘watch’ command to exit after a change in output is detected, use the -g command line option.
watch -g free
So anytime the memory consumption in the system changes, the watch command will exit.
As you’d have understood by now, watch is an excellent way to keep track of changes, be it on system-level, directory level, or even file level. Here, in this tutorial, we have discussed several key command-line options of ‘watch.’ Once you are done practicing these, head to the tool’s man page to learn more.