Sometimes, while working on the Linux command line, you might want to execute a command repeatedly to track any output change. Luckily, there is a command-line utility that lets you do this.
With the Linux watch command, you can track the changes in the output from time to time. It is beneficial for reflecting the real-time view of events on an operating system.
The watch command comes installed, by default, on nearly all Linux distributions. It is useful when monitoring changes in a command output over time. So instead of reading the whole output, you can keep an eye on the changes.
How to Use watch Command in Linux
The syntax for the watch command is as follows:
watch [OPTIONS] COMMANDCode language: CSS (css)
Where:
[OPTIONS]: Adding an option changes how thewatchcommand behaves.[COMMAND]: A user-defined command you want to run repeatedly.
By default, the specified command will run every 2 seconds, and the watch command will run until interrupted by the user (pressing Ctrl+C).
For example, we can monitor the output of the date command every two seconds:
watch date

As a result, the watch command will temporarily clear all of the terminal content and start running the command at regular intervals. You can see the update interval and the executed command on the top left side of the screen header.
How to Change the Time Interval
We know that by default, the command runs every 2 seconds. If we pass the -n (--interval) option to the watch command, we can specify the update interval. You will need to set the amount of time in seconds.
I’ll continue to use the date command as an example. The following command will run the date command every second.
watch -n 1 date

Highlighting the Difference
If you’re running a command that prints out a big output, keeping an eye for changes can become pretty troublesome. Fortunately, the watch command can highlight the difference between the previous and current output.
We can use the -d (--difference) option to view the changing output. This option will highlight the changes.
watch -n 1 -d date

However, if the interval between the updates is very short, for example, -n 0.1, it will be challenging to review differences. Therefore, you must set a reasonable update interval.
Hide Header in Output
The -t (--no-title) option is used to turn off the header showing the interval, command, and the current time at the top of the display if you’re not interested in seeing this portion.
watch -t date

Exiting When Change Occurs
By default, the watch command keeps running until it is interrupted manually by the user (Ctrl+C). However, sometimes instead of highlighting the changes, you’d prefer the watch command to exit when a change is detected completely.
You can set watch to exit when the output from the command changes by using the -g (--chgexit) option.
watch -g date
The above example stops the watch command any time there are changes in the date command output, which in this case means after 2 seconds.
Beep on Error
The watch command can also give a beep sound if an update fails. It uses the beep package to play a sound alert if the output update fails due to an error.
watch -b incorrect-command
Conclusion
Now you know all about the watch command on Linux. Although it’s a simple program, it can be helpful if you use it properly.
Please find out more about various options in watch on its command line manual page.
