Linux watch command
Linux watch command
The Linux watch
command is a small but powerful utility that runs a command repeatedly and shows you its output full-screen, refreshing at a regular interval. It’s great for monitoring changing system state (logs, disk usage, processes) without typing the same command over and over.
When you run watch <command>
, the terminal clears and the given command runs repeatedly. By default the command runs every 2 seconds and the screen updates with the latest command output. This makes it easy to observe a value or status over time.
Basic syntax
The general syntax of the command is as follows:
$ watch [options] <command>
Common options
-n <seconds>
— change the interval (e.g.-n 1
to run every 1 second).-d
— highlight differences between successive updates (useful to quickly spot changes).-t
— turn off the header (removes the summary line showing interval, time, and command).-g
— exit when the command’s exit status changes (handy for waiting until something finishes).-b
— beep if the command has a non-zero exit status.
Examples
Monitor disk free space every 5 seconds
$ watch -n 5 df -h
Output
Watch a directory listing and highlight changes
$ watch -d ls -l /var/log
Follow the last lines of a log-like file (refresh every 1 second)
$ watch -n 1 'tail -n 20 /var/log/syslog'
Exit when a process disappears (useful for CI or jobs)
$ watch -g pgrep -f my_long_running_process
Best practices
- Quoting commands: If your command contains pipes, redirection, or multiple arguments, quote the whole command so
watch
evaluates it correctly:watch -n 2 "ps aux | grep nginx"
- Don’t hammer the system: very short intervals (e.g. <0.5s) can overload the system for heavy commands. Use reasonable intervals for expensive queries.
- Non-interactive commands only:
watch
runs commands non-interactively. Commands that require user input won’t work as expected. - Terminal size matters: Output longer than the terminal height will be truncated. Use
less
or redirect to a file for detailed exploration.
Keyboard shortcuts
Ctrl-C
— stopwatch
and return to the prompt.- Terminal scrolling still works in most terminals, but the display is controlled by
watch
and is refreshed each interval.
Where watch
comes from
watch
is part of the procps (or procps-ng) package on many Linux distributions. It’s typically installed by default on desktop/server distributions; if it’s missing, install the package using your distro’s package manager (for example, sudo apt install procps
on Debian/Ubuntu or sudo yum install procps-ng
on some RPM-based systems).
Real-world Use cases
- Watch memory, CPU, and disk changes during a heavy workload:
watch -n 2 free -h
. - Observe file creation in a directory while a process writes output:
watch -d ls -lh /path/to/dir
. - Wait for a service to start up or a command to finish using
-g
and react when it does.
watch
is a tiny monitoring helper that saves you from repeating commands and lets you watch system changes live. It’s simple, lightweight, and when combined with short shell commands it becomes a fast diagnostic tool in the toolbox of any Linux user.