When managing an Ubuntu server, you frequently need to check whether a process is still running, how much disk space is being used, or whether a network connection is active. Normally, you would type a command like df -h, read the output, wait a few seconds, press the up arrow to re-run the command, and repeat this tedious cycle indefinitely. If you are monitoring something that changes rapidly, such as memory usage during a software deployment, this manual approach is far too slow and error-prone.
The watch command solves this problem completely. It automatically re-executes any command at a regular interval (every two seconds by default) and displays the updated output in a clean, full-screen terminal view. Instead of repeatedly typing the same command, you simply tell watch what to monitor, and it continuously refreshes the results for you until you stop it.
Basic Syntax of the watch Command
The basic syntax is straightforward:
watch [options] command
For example, to monitor disk usage every two seconds:
watch df -h
The terminal will clear and display the output of df -h. Every two seconds, watch will re-run the command and update the display. The top of the screen shows the command being executed and the current time, making it easy to track how long you have been monitoring.
To stop watch, press Ctrl + C.
How to Change the Refresh Interval
The default refresh interval is two seconds, which is appropriate for most monitoring tasks. However, you can change this to any interval using the -n flag followed by the number of seconds.
To refresh every 5 seconds:
watch -n 5 df -h
To refresh every half-second (0.5 seconds) for near-real-time monitoring:
watch -n 0.5 free -m
Be cautious with very short intervals. Setting the refresh rate to 0.1 seconds on a resource-intensive command could itself consume noticeable CPU and I/O resources.
How to Highlight Changes Between Refreshes
One of the most useful features of watch is the ability to highlight differences between the current output and the previous output. This makes it immediately obvious when a value changes, which is invaluable when monitoring rapidly changing data.
Use the -d (differences) flag:
watch -d free -m
Each time the output refreshes, any values that have changed since the previous refresh will be highlighted (usually in reverse video or bold, depending on your terminal). This is extremely helpful when monitoring memory usage, because you can instantly see which memory values are fluctuating.
To make the highlighting permanent (so that all values that have ever changed remain highlighted), use:
watch -d=cumulative free -m
Cumulative highlighting is useful when you want to identify every value that has changed during an entire monitoring session.
Practical Examples of the watch Command
Monitor Memory Usage
watch -n 1 free -h
This refreshes the memory statistics every second in human-readable format. Ideal for tracking whether a deployment is consuming excessive RAM.
Monitor Disk Space
watch -n 10 df -h
This checks available disk space every 10 seconds. Useful when running a large data import and you need to ensure the drive does not fill up.
Monitor Active Network Connections
watch -n 2 ss -tuln
This displays all active TCP and UDP listening sockets, refreshing every two seconds. Helpful when deploying a new service and waiting for it to start listening on a specific port.
Monitor a Specific Process
watch -n 1 "ps aux | grep nginx"
Note: When the command includes pipes (|), redirections (>), or other shell operators, you must wrap the entire command in quotation marks. Without the quotes, watch will only execute the first part of the command.
Monitor the Contents of a Directory
watch -n 5 ls -lh /var/log/
This displays the contents of the /var/log/ directory every five seconds, showing file sizes and modification times. Useful for checking whether log files are growing unexpectedly.
Monitor the Number of Files in a Directory
watch -n 3 "ls /tmp | wc -l"
This counts the number of files in the /tmp directory every three seconds. Helpful when a process is generating temporary files and you want to track how quickly they accumulate.
Monitor System Load Average
watch -n 2 uptime
This displays the current system uptime and load averages, refreshing every two seconds. The load average values indicate how busy the CPU is over the last 1, 5, and 15 minutes.
How to Exit watch When a Condition Is Met
By default, watch runs indefinitely until you press Ctrl + C. However, you can configure it to exit automatically when the output of the monitored command changes, using the -g (chg/exit on change) flag.
watch -g ls /home/user/downloads/
This will monitor the contents of the downloads directory. The moment a new file appears (or an existing file is deleted), watch will detect that the output has changed and automatically exit. This is extremely useful when you are waiting for a specific file to arrive, such as a completed download or a generated report.
How to Remove the Header Information
By default, watch displays a header at the top of the screen showing the interval, the command being run, and the current time. If you want a cleaner display (for example, when using watch as part of a monitoring dashboard), you can hide the header using the -t flag:
watch -t -n 1 date
This displays only the output of the date command, refreshing every second, without any additional header information.
Common Mistakes to Avoid
- Forgetting quotation marks with pipes and redirections: If your command includes
|,>,>>, or;, you must wrap the entire command string in double quotes. Otherwise,watchwill only execute the portion before the pipe. - Using aliases: The
watchcommand does not expand shell aliases. If you have an alias likell='ls -la', runningwatch llwill fail. Use the full command instead:watch ls -la. - Colour output: Some commands produce coloured output using ANSI escape codes. By default,
watchdoes not interpret these codes. Use the-c(color) flag to enable colour support:watch -c -n 1 "dmesg --color=always | tail -20".
Combining watch with Other Monitoring Tools
The watch command is not a replacement for dedicated monitoring tools like htop, btop, or Prometheus. It is a lightweight, flexible utility that is ideal for ad-hoc monitoring tasks where you need to keep an eye on a specific value without setting up a complex monitoring stack.
For quick server health checks during deployments, troubleshooting sessions, or maintenance windows, watch is one of the most practical tools available in the Ubuntu terminal. It requires no installation, no configuration, and works immediately on virtually every Linux distribution.