Linux Command Reference ยท Processes and Signals
Linux watch command
Reference for the Linux watch command: rerun a command every few seconds, highlight differences, stop on change or error, and a plain Bash loop that works anywhere.
watch runs a command over and over and shows its output full-screen, refreshing it every two seconds by default. It is good for following a file that grows, a directory filling up or a service that is starting. It is an interactive terminal tool: it takes over the screen and runs until you press Ctrl+C.
Because watch needs a terminal, the live runner on this site cannot show its screen. The first example is read-only. The second one reproduces the idea with a plain Bash loop, which also works in scripts, logs and over limited connections.
watch [OPTION]... COMMAND
Try it
Read-only, on a real terminal:
watch -n 1 -d 'ls -l | wc -l' # count files every second, highlight changes watch -g 'test -f done.flag' # exit when the output changes
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
watch [-bdeghnptx] [-c] [-n SECONDS] [-d[=cumulative]] [-q] COMMAND
| Option | Description |
|---|---|
-n SECONDS |
Interval between runs (default 2, minimum 0.1). |
-d |
Highlight what changed since the previous run. -d=cumulative keeps every change highlighted. |
-t |
Hide the header line. |
-g |
Exit when the output changes. |
-e |
Stop if the command returns a non-zero status. |
-c |
Interpret ANSI colour sequences. |
-x |
Run the command with exec instead of sh -c. |
-b |
Beep when the command fails. |
-p |
Try to run at precise intervals regardless of how long the command takes. |
Returns: 0 on a normal exit (Ctrl+C), or the failing command's status with -e.
Usage notes
- The command goes through
sh -c. Quote the whole command if it has pipes or redirections:watch 'df -h | grep sda'. - Quotes matter:
watch ls -l | wcpipes watch's own screen output intowc; you probably wantedwatch 'ls -l | wc'. - Interval is the pause after a run. A slow command makes the real cycle longer; use
-pto keep a steady rhythm. - Use it, do not log it. The output is a changing screen. For a record over time, use a loop with
dateand>>. - Good uses:
watch -d free -m,watch -n 5 'ss -s',watch kubectl get pods,watch -n 1 'ls -l /var/spool'. - Not in scripts. Without a terminal
watchfails. Use the loop from the example instead. - Not part of coreutils. It ships in the
procpspackage; on macOS install it separately.