The watch
command in Bash is a useful utility that allows you to execute a specified command at regular intervals. This is particularly helpful for monitoring changes in the output of commands, such as system resource usage or file changes.
The basic syntax of the watch
command is as follows:
watch [options] [arguments]
-n, --interval <seconds>
: Specify the interval in seconds between command executions. The default is 2 seconds.-d, --differences
: Highlight the differences between successive outputs.-t, --no-title
: Disable the display of the header showing the command being executed and the interval.-x, --exec
: Execute the command with the specified arguments.To monitor the disk usage of the root directory every 5 seconds, you can use:
watch -n 5 df -h /
To see the list of running processes and update it every 2 seconds:
watch ps aux
To monitor the last 10 lines of a log file and see updates:
watch tail -n 10 /var/log/syslog
To highlight changes in the output of a command, such as checking the contents of a directory:
watch -d ls -l /path/to/directory
-n
option to adjust the refresh rate according to your needs; a shorter interval can provide real-time monitoring.watch
with commands that generate dynamic output to effectively track changes.-t
option if you want a cleaner output without the header, which can be useful for scripts or logging.