The Linux watch command provides a useful means of executing commands periodically. Many system admins use this simple tool to get live feedback from frequently used Linux terminal commands. Moreover, it also allows us to locate changes in command outputs in real-time. In this guide, we have discussed the various use cases of the watch utility and illustrate several useful usages of this tool for our readers. So, if you were looking for a way to run commands repeatedly, continue reading with us. You will learn to use one of the most useful yet often overlooked Linux commands.
Useful Examples of Linux Watch Command
Using the Linux watch utility is a simple and straightforward task. It follows a simple syntax, and the lack of complex options eliminates the necessity of remembering too many staff. Take a look at the following examples to learn how watch works and how to implement it in your everyday activities.
1. Basic Usage of Linux Watch Command
When used without any command-line arguments, the watch utility will run the specified command every two seconds. Check out the following simple example to see how it works.
$ watch date
If you run the above command in your favorite Linux terminal emulator, it will print the output produced by the date command on your screen. The output will be refreshed every 2 seconds. The top part of the screen will display which command is being run by watch and the active interval period.
2. Specify the Update Interval
Users can specify the interval period for their watch program very easily by using the -n option. You will need to provide the new interval time (in seconds), followed by this option. The following illustration shows how it works in practice.
$ watch -n 5 date
This command will run similarly to the above command but will only update the result every five seconds. The top-right corner of the screen will display the current time after each refresh. You can also use the long-form –interval in place of the -n option, as shown below.
$ watch --interval 5 date
Use the Ctrl+C combination to quit the watch command and return to your terminal session.
3. Highlight Differences Between Each Update
The watch utility makes it very simple to spot differences between the old and updated output. You can make use of the -d flag to switch on difference highlighting. The below command should provide a practical illustration of this option.
$ watch -n 5 -d date
This command will run the date utility every five seconds and highlight the changes in output on your terminal screen. There is a long-style syntax for the -d option called –differences, which is illustrated below.
$ watch -n 5 --differences date
The -d -or –differences options also take an optional argument called ‘permanent‘. When you specify this parameter, the cursor highlighter will become permanent instead of being on and off.
$ watch -n 5 --differences=permanent date
4. Turn of Title and Headers
You should notice by now that the Linux watch command shows information like the command name that’s being run, the interval, and the current time at the top of the screen. You can use the -t option of watch to disable this information. Check out the following example to see how it works.
$ watch -t date
As expected, this will only display the output produced by the date command. It also strips the additional blank line you’d see when not using the -t option. Users can also choose to use the long-style syntax –not-title for this option, as demonstrated below.
$ watch --no-title date
5. Exit Watch on Error
You can specify your watch command to exit whenever there is an error produced by the command it is running. Simply add the -e option, and watch will exit when the output contains an error. The below command illustrates this using a very simple example.
$ watch -e exit 99
If you copy the above Linux watch command and run it, it will display a prompt saying that the command has a non-zero exit status. Pressing any key on your keyboard will exit the execution. Note that commands that execute without any error exit with a zero status code. So, having a non-zero code means that the command produced an error.
$ watch --errexit exit 99
The –errexit option is an alternative syntax for -e.
6. Exit Watch on Changes in Output
The -g flag of the watch utility signals an exit whenever there is a change in the output. This can be handy for people who want to set a simple monitoring session right from their terminal window. Take a close look at the below example to learn how this works.
$ watch -g date
This command will run for two seconds, and as soon as the output updates, it will exit. This was a rather simple illustration, but you should be able to get the hang of it. For example, you could use this command to monitor network resources and get notified when something changes.
$ watch --chgexit netstat --al
The above example uses the long-form of the -g option, which is –chgexit.
7. Notify on Error
The -b option of the Linux watch utility rings a beep tone whenever the command exits with a non-zero status code. As discussed already, a non-zero status code usually points to an error or failure to execute the given command. This makes it easy to spot such problems.
$ watch -b date
This command will give a beeping sound if the output of the date command contains an error. Although the date is unlikely to cause any error, it illustrates how the -b option can be used. You may also use the abbreviated syntax –beep in place of the -b option.
$ watch --beep date
However, the beep program must be installed on your system, or else watch will not be able to ring the sound.
8. Interpret Color Codes and Style Sequences
You can enable the interpretation of ANSI color codes and style sequences for the watch utility using either one of the -c or –color options. By default, watch does not interpret colors on its output. Check out the below example carefully to see how this option works.
$ watch -c echo "$(tput setaf 1)Welcome to UbuntuPit"
The output of this command contains the color-coded string “Welcome to UbuntuPit”. Now, remove the -c option and run the command once more. You will see that the string does not contain any colors this time around. The long-form syntax for this option is –color.
9. Run Commands in Precise Intervals
When you use the -n option for specifying command execution intervals, the fractional seconds tend to increase continuously. Although the commands seem to be running in every Nth second, it actually takes a fraction longer. You can eliminate this by using the -p option.
$ watch -n 3 -p echo "Hello World"
This command will run print the “Hello World” string every 3 precise seconds. You can use ntptime to locate the fractional differences in running intervals. The below example uses the long-styled syntax –precise.
$ watch -n 3 --precise echo "Hello World"
10. Use Exec Instead of sh -c
You can pass the command given to watch to exec instead of the more standard sh -c. This allows users to eliminate the need -f using quotes. Check out the following simple example to see how this works.
$ watch -n 6 -x echo hello world
Another benefit of using exec over sh is that it eliminates wrapper processes from the computer’s memory with the actual process. The long-styled form for this option is –exec.
$ watch -n 6 --exec echo hello world
11. Display Mails using Linux Watch Command
One great example of the watch utility is checking incoming emails. The following example shows users how to use watch for monitoring emails directly from their terminal session.
$ watch -n 60 from
The above command will check for new email messages once per minute. It leverages the from utility to check whether there are any new messages or not.
12. Monitor Directory Contents for Changes
The following simple example illustrates how we can use the watch utility to monitor file system directories for content changes. It will print out the directory listing and highlight changes in the contents.
$ watch -d ls -l
We can use the Linux grep command to filter out the output for a specific user only. The below command looks for changes made by the user ‘ralph’.
$ watch -d 'ls -l | grep ralph'
You can place larger commands inside quotes or use the backslash if looking for a cleaner way.
$ watch -d \ > ls -l | grep 'ralph'
13. Monitor CPU Temperature Using Watch
If you are using an old laptop or a rusty computer with heating issues, you would need to keep the temperature in check. You can use the watch utility in conjunction with the sensors tool to monitor your computer’s temperature. Take a close look at the below example to see how this works.
$ watch -n 60 sensors
This command will check the core temperature of your laptop or desktop once every minute. It will also let you know whether the current temperature is critical so you can shut down the system. Check out our previous guide on laptop overheating issues to learn how to solve the problems.
14. Display the Version information
The version information of a tool helps admins determine the origin of the program and find patches for bugs. You can view the version data for the watch utility using one of the following simple commands.
$ watch -v $ watch --version
15. Display the Help and Manual Page
The help page of Linux terminal commands provides summarized information of all possible command-line options and their usage. Feel free to check the help page of the Linux watch command if you want quick information for a specific option.
$ watch -h $ watch --help
Both of these options will display the help page and then exit. On the other hand, consult the manual page whenever you want to learn about a certain option in-details
$ man watch
Ending Thoughts
The Linux watch command is a simple yet useful tool that has a large number of use cases. Admins can use it to set up personalized monitoring for frequently used terminal commands. In this guide, we have discussed all of the options available to watch. We have also illustrated several practical examples to help readers master this tool. Hopefully, we could provide you all the essential information you were looking for regarding this handy little tool. If you have any further questions or suggestions, leave them in the comment section, and we will get back to you.