Linux Watch Command
3 min read
In this tutorial, we will introduce you to the
watch is used to run any arbitrary command at regular intervals and displays the output of the command on the terminal window.
It is useful when you have to execute a command repeatedly and watch the command output change over time. For example, you can use the
watch command to monitor the system uptime or disk usage.
watch utility is a part of the
procps-ng) package which is pre-installed on nearly all Linux distributions.
How to Use the
The syntax for the
watch command is as follows:
watch [OPTIONS] COMMAND
To better illustrate how the
watch command works let’s run the
As you can see in the image above, the
watch command will temporarily clear all of the terminal content and start running the provided command at regular intervals. When used without any option
watch will run the specified command every two seconds.
On the top left side of the screen header you can see the
watch update interval and the executed command (
Every 2.0s: date), while on the top left side
watch shows the current time and date. If you want to turn off the header use the
The output of the specified command is shown on the screen and regularly updated every two seconds.
To exit out of the
watch command, just hit the
Ctrl+C key combination. You can also set
watch to exit when the output from command changes by using
In the following sections, we’ll go over the most commonly used
watch command options.
How to Change the Time Interval
What if the default update interval of two seconds is not suitable for your use-case?
--interval) option followed by the desired number of seconds allows you to change the time interval between updates:
watch -n INTERVAL_IN_SECONDS COMMAND
For example, to monitor your disk space usage with the
df command and refresh the screen every five seconds on you would run:
watch -n 5 df -h
Highlighting the Difference Between Updates
--difference), option will cause
watch to highlight the changes between successive updates.
watch -d COMMAND
Let’s say you want to monitor the system uptime by running the
uptime command and to highlight the changes. The command would be:
watch -d uptime
If you want the highlights to be sticky, pass
=cumulative to the
-d option. This means that all values that have ever changed will stay highlighted.
watch -d=cumulative COMMAND
Commands with Pipes
If you want to execute a command that contains pipes you need to enclose the command in single or double quotes. If you don’t enclose the full command watch will run just the first command and then pipe its output to the next command in the pipeline.
watch 'COMMAND_1 | COMMAND_2'
For example, the following command will monitor the number of active connections on port
80 using a combination of the
watch "netstat -anp | grep -c ':80\b.*LISTEN'"
By now you should have a good understanding of how to use the Linux
watch command. You can always view all available
watch command options by typing
man watch in your terminal.
If you have any questions or feedback, feel free to leave a comment.