
- What is watch?
- Basic Linux watch command syntax
- Basic usage of the watch command
- Checking version information
- Watch options command
- Using a standard watch command with no options
- Setting a custom time interval
- Showing changes between update intervals
- Turn off the header from a watch command
- Piping multiple commands into watch
- Combining watch with the tail command
- Have the watch command exit on change of output
- Having the watch command display colored output
- Have the watch command alert you for file changes
- Conclusion
What is watch?
The Linux watch command line utility is installed by default and it allows for real time system monitoring on the command line interface.
This command allows for you to monitor command output in regular intervals and during each interval the output of your command refreshes.
In the past I’ve personally used the watch command many times to monitor system resource usage during various tasks.
For example I have used watch to monitor disk usage while an rsync command was running in the background on a server.
This was very useful at the time as it helped in preventing the disk from reaching full capacity during the file copy since the command periodically would show the updated disk usage.
During that if the disk space reached close to 100% I was able to see this while using watch and then could cancel the rsync. After that I’d be able to explore other options for how to proceed with the sync, such as cleaning up the disk before running it again.
The Linux watch command can be used in combination with tons of other commands so it can be crafted to suite your specific needs.
In this article we’ll be covering the basic usage of the watch command, the common watch command options, and practical examples of how we can combine multiple commands with watch.
Basic Linux watch command syntax
The basic syntax for using the watch command is pretty straight forward. Overall you will use ‘watch’ in the beginning of a command followed by an option and then the command you’d like to watch.
Such as in this example:
$ watch [options/flags] [command to watch]
Basic usage of the watch command
Checking version information
You can view the version installed with the following command:
$ watch -v
Watch options command
You can always check the ‘man pages’ for more information on the options offered with watch but you’re also able to just run the following watch options command:
$ watch -h

Using a standard watch command with no options
Let’s say you want to monitor disk usage during a file copy to make sure the disk doesn’t reach 100% capacity.
In such an example you could use this simple watch command without any flags/options and by default it will refresh the command output every two seconds:
$ watch df -h
Setting a custom time interval
On the other hand lets say you want to have the watch command update the output every 5 seconds rather than the two second default.
In this case you could set a custom time interval to update a commands output with use of the -n flag which is used for setting the desired time interval for the refresh rate.
Using the following specified command in terminal would watch the ‘df -h’ command and refresh it’s output every 5 seconds since we’re setting -n to 5:
$ watch -n5 df -h
The output of the above disk space command would show the following output:

In the output displayed above we see the watch command header showing the refresh rate or update interval (5 seconds), hostname of your system, current system date, and then output of the df -h command we ran with watch.
Showing changes between update intervals
You can also have the command show changes between each update interval with use of the -d (–difference) option/flag.
With the specified command we will see that during each update it will highlight changes within a free -m command where we’re monitoring memory usage:
$ watch -d free -m
From the below screen shots which were taken at 3 different update intervals we see how the command highlights differences between each refresh interval:



Shown above we see that in each screen shot the command updates us at each interval by highlighting the changes in values.
Above it shows the memory “used” changes are being highlighted at each interval while the value fluctuates from 13,443MB to 13,460MB and then to 13,462MB.
Turn off the header from a watch command
If would prefer to turn off the header included in your output while using watch you can use the -t flag.
So as example in this command (before using -t) we see the time update interval at the top, command running, system name, and current time when using free -m to monitor memory usage:
$ watch free -m

And now when we add in the -t flag we will see how that information is removed from the displayed output:
$ watch -t free -m

Piping multiple commands into watch
In the below example we’ll combine a couple commands with watch to monitor processes running under a specific user named “bodonald”:
$ watch "ps faux | grep bodonald"

Combining watch with the tail command
To expand a bit further on the previous section we’ll combine ls and tail into a watch command to view the most recent files in a directory:
$ watch "ls -ltr /home/bodonald/Downloads | tail -20"
Have the watch command exit on change of output
There could be instances where you’d want to use watch to monitor a command but then exit if the output changes from that command.
This can be done when utilizing the -g flag in a watch command such as below:
$ watch -g df -h
From the above using -g in this example will cause the command to exit once the output of ‘df -h’ changes.
Having the watch command display colored output
Depending on your terminal settings you can have watch display its output in color with use of the -c option/flag such as in the following example where we’re monitoring memory usage:
$ watch -c free -m
This personally does not work for me as I do have a custom theme and colors set for my terminal profile but if you’re using the default settings this would alter the output to show in color.
Have the watch command alert you for file changes
Below is a simple ls command with watch but we’ll be adding in the -b flag which alerts you with a “beep” noise if changes are detected on files within the /home/user/html directory:
Note: the “beep” alert only works if the beep package is installed
$ watch -b ls -lah /home/user/html
Conclusion
Within Linux it can be very useful to have the watch command in your tool belt. There are many different use cases for watch and it can be crafted into more complex commands to suite your needs.
Leave a Reply