In this article, I will take you through 8 Useful Linux watch command in Examples. watch command is a very useful command to track any changes in the output of Linux commands like top, date, df, du, sar, netstat, ss etc. Linux watch command will be available by default in most of the Linux based systems hence you don't have to install it separately. Here we will go through multiple examples of Linux watch command to understand the usage of this command in different places.
watch [options] command
Linux Watch Command Examples
Example 1. Check watch command version
If you want to check the watch command version then you need to run
watch -v command as shown below.
[root@localhost ~]# watch -v watch from procps-ng 3.3.10
-v : Display version information and exit.
rootuser to run all the below commands.You can use any user with
sudoaccess to run all these commands.For more information Please check Step by Step: How to Add User to Sudoers to provide
sudoaccess to User.
Example 2. Check date command output every 5 sec
If you want to update the date command output every 5 seconds then you need to specify the secs value in argument with
-n option as shown below.
[root@localhost ~]# watch -n 5 date Every 1.0s: date Tue Apr 28 00:43:32 2020 Tue Apr 28 00:43:32 EDT 2020
-n : Specify update interval.
Example 3. Create a Clock using Linux watch command
If you want to create a clock using Linux watch command where time will be updated every second which basically means a clock is running then you need to use
watch -n 1 date command as shown below. This is one of the interesting example of Linux watch command which can be used in many places.
[root@localhost ~]# watch -n 1 date Every 1.0s: date Mon May 4 00:52:36 2020 Mon May 4 00:52:36 EDT 2020
Example 4. Track the difference in updates
If you want to track in difference in output updates then there is an option
-d which can be used with Linux watch command as shown below. Running this command will show a marker which will highlight the values which keeps changing in the output.
[root@localhost ~]# watch -d free -m Every 2.0s: free -m Tue May 5 12:52:57 2020 total used free shared buff/cache available Mem: 990 148 717 6 124 706 Swap: 2047 0 2047
-d : Highlight the differences between successive updates.
Example 5. Turn Off the Header
If you want to turn off the header information in Linux watch command output, then
-t option can be helpful. It will only display the output without any header.
[root@localhost ~]# watch -t date Tue May 5 12:58:19 EDT 2020
-t : Turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
Example 6. Exit out if command output changes
If you want to exit out from the Linux watch command the moment output changes then you can use
watch -n 5 -g date command as shown below.
[root@localhost ~]# watch -n 5 -g date Every 5.0s: date Tue May 5 13:01:08 2020 Tue May 5 13:01:08 EDT 2020
-g : Exit when the output of command changes.
Example 7. Make Watch attempt to run command every Interval Seconds
If you want to make watch attempt to run command every interval seconds then you need to use
-p option with Linux watch command as shown below.
[root@localhost ~]# watch -p df -h Every 2.0s: df -h Wed May 6 13:47:39 2020 Filesystem Size Used Avail Use% Mounted on devtmpfs 484M 0 484M 0% /dev tmpfs 496M 0 496M 0% /dev/shm tmpfs 496M 6.8M 489M 2% /run tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/mapper/centos-root 37G 1.5G 36G 4% / /dev/sda1 1014M 193M 822M 19% /boot tmpfs 100M 0 100M 0% /run/user/0
-p : Make watch attempt to run command every interval seconds.
Example 8. Check Other Options of Linux watch command
You can use
--help option with Linux watch command to check all the other available options that can be used.
[root@localhost ~]# watch --help Usage: watch [options] command Options: -b, --beep beep if command has a non-zero exit -c, --color interpret ANSI color and style sequences -d, --differences[=<permanent>] highlight changes between updates -e, --errexit exit if command has a non-zero exit -g, --chgexit exit when output from command changes -n, --interval <secs> seconds to wait between updates -p, --precise attempt run command in precise intervals -t, --no-title turn off header -x, --exec pass command to exec instead of "sh -c" -h, --help display this help and exit -v, --version output version information and exit For more details see watch(1).
--help : Display help text and exit. More information on Watch Command Man Page.