Quick take: The watch command re-runs a command at a set interval and shows its latest output full-screen. watch df -h refreshes every 2 seconds; -n 1 sets a 1-second interval and -d highlights what changed.
Introduction
The watch command repeatedly runs another command and displays its output, refreshing the screen each time. It turns any one-shot command into a live monitor — perfect for watching disk usage, queue lengths, file counts, or service status change in real time without writing a loop.
Syntax
The basic syntax of the watch command is:
watch [OPTIONS] COMMANDCommon Options and Parameters
The most useful options and parameters for the watch command:
| Option | Description |
|---|---|
| -n SECS | Set the refresh interval (default 2 seconds). |
| -d | Highlight the differences between updates. |
| -t | Hide the header line. |
| -g | Exit when the output changes. |
| -b | Beep if the command has a non-zero exit. |
| -x | Pass the command to exec without a shell. |
Practical Examples
Real watch commands you can run today:
# Watch disk usage update
watch df -h
# Refresh every second
watch -n 1 free -h
# Highlight what changes
watch -d 'ls -l /var/spool/mail'
# Monitor a process count
watch -n 2 'ps aux | grep -c nginx'
# Watch a directory's file count
watch 'ls /incoming | wc -l'
# Exit when output changes
watch -g ls /tmp/readyTips and Best Practices
- Quote the command when it contains pipes or globs (
watch 'ls | wc -l') so watch runs the whole pipeline, not just the first part. - Use
-dto highlight changes, which makes it easy to spot what moved between refreshes. -gturns watch into a simple wait-for-change trigger, exiting the moment the output differs.
Final Thoughts
watch converts any command into a live dashboard, re-running it on an interval and refreshing the screen. Set the cadence with -n, highlight changes with -d, and quote piped commands so the whole pipeline runs. It is the quickest way to monitor something changing without scripting a loop, complementing top and tail -f.
FAQ: watch Command in Linux
How do I run a command repeatedly in Linux?+
Use watch command, which re-runs it every 2 seconds and shows the latest output. Set a different interval with -n, for example watch -n 5 command for every five seconds.
How do I change the watch interval?+
Use -n with seconds: watch -n 1 command refreshes every second, and decimals like -n 0.5 are allowed for faster updates.
How do I highlight changes with watch?+
Add -d (difference): watch -d command highlights the parts of the output that changed since the last refresh, making movement easy to spot.
How do I watch a command with a pipe?+
Quote the whole command: watch 'ps aux | grep nginx'. Without quotes, watch would only run ps and the shell would apply the pipe to watch's output.
How is watch different from a loop?+
watch handles the timing, screen clearing, and full-screen display for you, and can highlight changes. A while loop with sleep works too but needs manual formatting and clearing.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support