watch Command: Monitor Output at Intervals

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 command reruns 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.)
  • watch ships in the procps-ng package and is preinstalled on most distros
  • It is not included on macOS by default (use brew install watch if 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.

Lina: Senpai, I keep typing ls -l over and over to see if a file is growing. This is exhausting.
Linny-senpai: That is exactly what watch is for. It reruns the same command automatically and keeps refreshing the screen, so you do not have to hammer the keyboard.
Lina: It repeats automatically? How do I use it?
Linny-senpai: Just put 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.

Lina: Let me try it with ls -l.
Linny-senpai: Go for it. Type this:
watch ls -l
Linny-senpai: You should see a header like this at the very top of the screen:
Every 2.0s: ls -l                 host: Fri Jun  5 19:30:00 2026
Lina: "Every 2.0s" means every 2 seconds, right? And below it is the usual ls -l output!
Linny-senpai: Exactly. Every 2 seconds the content refreshes to the latest state. If a file is added or its size changes, you see it update right there.

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 example watch -n 5 command runs every 5 seconds. The minimum is 0.1 seconds.

Lina: Two seconds feels a bit fast. Can I slow it down?
Linny-senpai: Use -n to specify the seconds. For every 5 seconds:
watch -n 5 ls -l
Lina: What if I want it faster instead?
Linny-senpai: You can use decimals. -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=permanent keeps cumulative changes highlighted from the first iteration.

Lina: When the output is long, it is hard to spot what changed.
Linny-senpai: That's where -d helps. It highlights the parts that differ from the previous refresh.
watch -d ls -l
Lina: The changed parts light up! Now I won't miss them.
Linny-senpai: You can go further with -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.

Lina: I wanted to monitor a file's line count, so I typed watch ls | wc -l, but it behaved strangely.
Linny-senpai: That's a classic trap. Without quotes, the shell reads it as "pipe the output of watch ls into wc -l." To pass the whole ls | wc -l to watch, write it like this:
watch 'ls | wc -l'
Lina: I see, the quotes make ls | wc -l go to watch as one piece.
Linny-senpai: Right. The same applies to redirection > 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: -t removes the header, -c reproduces colored output, and -g exits the moment the output changes.

Lina: Can I get rid of that "Every 2.0s..." header at the top?
Linny-senpai: Yes, with -t (--no-title). Use it when you want to see only the output, cleanly.
watch -t ls -l
Lina: Also, with a colored command like ls --color, the colors disappear.
Linny-senpai: Add -c (--color) and watch will render the ANSI colors the command produces.
watch -c ls --color=always
Linny-senpai: One more handy one is -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 + C to end the session and return to the normal prompt.

Lina: It keeps running. How do I stop it?
Linny-senpai: Press Ctrl + C (hold Control and press C). That ends watch and returns you to your usual prompt.
Lina: It returned! I was worried because it just kept going.
Linny-senpai: watch repeats forever until you stop it. Once you're done checking, stop it with 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.

Lina: When would I actually use this?
Linny-senpai: Anywhere you want to keep an eye on change. Here are some common examples.
# 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'
Lina: So it's the go-to tool for "I want to see a number change over time."
Linny-senpai: Exactly. But if you just want to follow log lines as they stream, 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.

Next Reading