watch Command: Monitor Output at Intervals
What You'll Learn
- How to rerun the same command at a fixed interval with
watch - The two most common options:
-n(interval) and-d(highlight changes) - How to avoid the quoting pitfall when using pipes or redirection
- How to exit watch cleanly (Ctrl + C)
Quick Summary
watch commandreruns the command every 2 seconds and refreshes the screen- Change the interval with
watch -n 5 command(every 5 seconds) - Highlight what changed with
watch -d command - When using a pipe
|, wrap the whole thing in quotes:watch 'ls | wc -l'
Prerequisites (target environment)
- OS: Linux (Ubuntu / Debian / RHEL family, etc.)
watchships in theprocps-ngpackage and is preinstalled on most distros- It is not included on macOS by default (use
brew install watchif needed)
1. What Is the watch Command?
Conclusion: watch reruns a given command at a fixed interval (2 seconds by default) and redraws the latest output on screen. It is used to monitor change.
ls -l over and over to see if a file is growing. This is exhausting.watch is for. It reruns the same command automatically and keeps refreshing the screen, so you do not have to hammer the keyboard.watch in front of the command you want to monitor. Let's start with the simplest form.2. Basic Usage - Repeat a Command With watch
Conclusion: Just write
watch command. By default it runs every 2 seconds and shows the interval, the command, and the time in a header at the top.
ls -l.watch ls -l
Every 2.0s: ls -l host: Fri Jun 5 19:30:00 2026
ls -l output!The header at the top shows the interval, the running command, the hostname, and the current time. It also confirms that watch is still alive and running.
3. How to Change the Interval (-n option)
Conclusion: Use
-n seconds(or--interval) to set the interval. For examplewatch -n 5 commandruns every 5 seconds. The minimum is 0.1 seconds.
-n to specify the seconds. For every 5 seconds:watch -n 5 ls -l
-n 0.5 runs every half second, down to a minimum of 0.1 seconds. But a very short interval runs the command very often and uses more CPU, so use only the speed you actually need.watch -n 0.5 ls -l
A short interval like -n 1 runs the command that much more frequently. Running a heavy command (like summarizing a huge directory) at a short interval can cause high load, so be careful.
4. How to Highlight What Changed (-d option)
Conclusion:
-d(--differences) highlights what changed since the previous run.-d=permanentkeeps cumulative changes highlighted from the first iteration.
-d helps. It highlights the parts that differ from the previous refresh.watch -d ls -l
-d=permanent. It keeps any spot that ever changed highlighted, which is handy when you want to know later "what changed at some point while I wasn't looking."watch -d=permanent ls -l
5. The Quoting Pitfall With Pipes and Redirection
Conclusion: To include
|or>, wrap the whole command in quotes. Without quotes, the shell interprets them first and pipes watch's own output instead.
watch ls | wc -l, but it behaved strangely.watch ls into wc -l." To pass the whole ls | wc -l to watch, write it like this:watch 'ls | wc -l'
ls | wc -l go to watch as one piece.> and wildcards like *, anything the shell treats specially. When in doubt, wrap the entire command you pass to watch in single quotes.Rule of thumb: if you use a pipe or special symbol, write watch 'whole command'. Forgetting to quote is the most common watch mistake.
6. Hide the Header, Keep Colors, Stop on Change
Conclusion:
-tremoves the header,-creproduces colored output, and-gexits the moment the output changes.
-t (--no-title). Use it when you want to see only the output, cleanly.watch -t ls -l
ls --color, the colors disappear.-c (--color) and watch will render the ANSI colors the command produces.watch -c ls --color=always
-g (--chgexit). It exits watch the moment the output differs from the previous run, perfect for "tell me when something changes."watch -g ls -l
Common options at a glance
| Option | Meaning |
|---|---|
-n SEC |
Set the interval (default 2s, minimum 0.1s) |
-d |
Highlight changes since the last run |
-d=permanent |
Keep cumulative changes highlighted |
-t |
Remove the top header |
-c |
Reproduce the command's ANSI colors |
-g |
Exit when the output changes |
-x |
Run directly without sh -c (mind argument parsing) |
7. How to Exit watch
Conclusion: watch keeps running until you stop it. Press
Ctrl + Cto end the session and return to the normal prompt.
Ctrl + C (hold Control and press C). That ends watch and returns you to your usual prompt.Ctrl + C and you're fine.8. Common Use Cases
Conclusion: watch shines whenever you want to eyeball a number that changes over time: disk usage, processes, log counts, network connections.
# Monitor disk usage as it changes watch df -h # Monitor the count of a specific process (quote the pipe) watch 'ps aux | grep nginx' # Watch a log's line count grow watch 'wc -l /var/log/syslog' # Monitor network connection count every 5 seconds watch -n 5 'ss -tan | wc -l'
tail -f fits better. watch "re-photographs the whole screen periodically," while tail -f "streams new lines as they appear." Pick the right one for the job.