watch Command: Usage, -n Interval, -d Highlighting, and Exiting When Output Changes
The watch command is a Linux command that reruns a given command every 2 seconds and redraws its output across the whole terminal screen. Watching files on an AlmaLinux 9 server while changing them from another window showed that the -d option highlighted the characters that changed and the -g option ended watch after the output changed.
The output below comes from a working folder on an AlmaLinux 9.8 server, run as root; on a Debian 12 server only the version was checked. The host name in the header line at the top of the screen was replaced with the example value server1, and times are shown in UTC.
1️⃣ How to use the watch command
Write the command to rerun after watch, and with no options it runs that command every 2 seconds and replaces the screen with the new output. Watching date put a header line at the top of the screen, followed by an empty line and then the output of date.
watch dateEvery 2.0s: date server1: Thu Oct 29 02:28:07 2026
Thu Oct 29 02:28:07 UTC 2026The left side of the header line is the refresh interval and the running command; the right side is the host name and the time of the last run. Checking the same screen again 2 seconds later, the time in the header line and the date output had both changed.
Every 2.0s: date server1: Thu Oct 29 02:28:09 2026
Thu Oct 29 02:28:09 UTC 2026watch keeps running until you end it yourself, so end it with Ctrl+C. Pressing Ctrl+C cleared the watch screen and brought back the prompt. The exit code checked right after was 0.
echo $?0watch is used to rerun commands that print once and exit. To see CPU and memory use change process by process, the top command, which redraws its own screen, is the right tool.
watch is part of the procps-ng package. Check the version with [watch -v]; it was 3.3.17 on the AlmaLinux 9.8 server and 4.0.2 on the Debian 12 server.
watch -vwatch from procps-ng 3.3.17watch -vwatch from procps-ng 4.0.22️⃣ watch interval and highlighting options
Put a number of seconds after -n to change the refresh interval. Running [watch -n 1 date] changed the left side of the header line to Every 1.0s.
watch -n 1 dateEvery 1.0s: date server1: Thu Oct 29 02:28:15 2026
Thu Oct 29 02:28:15 UTC 2026According to the man page, the interval cannot be set shorter than 0.1 seconds, and smaller values are changed to 0.1 seconds. Both a period and a comma are accepted as the decimal point.
-d highlights the characters that differ from the previous refresh. While watching size.log in the working folder with [ls -l], one line was added to the file from another window.
watch -d -n 1 ls -l size.logEvery 1.0s: ls -l size.log server1: Thu Oct 29 02:28:53 2026
-rw-r--r-- 1 root root 7 Oct 29 02:28 size.logprintf 'line 2\n' >> size.logWhen the file size changed from 7 to 14, everything from the size column to the end of the line was highlighted. The number gained a digit, so every character after it shifted one place. On the next refresh the value stayed the same and the highlighting disappeared.
Every 1.0s: ls -l size.log server1: Thu Oct 29 02:28:54 2026
-rw-r--r-- 1 root root 14 Oct 29 02:28 size.logTo keep highlighting everything that changed since the first run, use [--differences=permanent] (per the man page).
-t removes the header line and the empty line below it, leaving only the command output.
watch -t -n 1 dateThu Oct 29 02:28:19 UTC 20263️⃣ watch command exit conditions and options
-g ends watch when the command output changes. Use it to wait for the contents of a status file to change before moving on to the next task.
watch -g -n 1 cat status.txtEvery 1.0s: cat status.txt server1: Thu Oct 29 02:29:12 2026
runningFrom another window, the command below changed the contents of status.txt to done.
printf 'done\n' > status.txtwatch then ended and the prompt came back, with exit code 0.
echo $?0-e freezes the screen when the command ends with a non-zero exit code. Watching a file that does not exist in the working folder with ls showed the ls error output, plus a notice on the bottom line of the screen saying that pressing a key exits.
watch -e -n 1 ls nofile.txtEvery 1.0s: ls nofile.txt server1: Thu Oct 29 02:29:18 2026
ls: cannot access 'nofile.txt': No such file or directory
command exit with a non-zero status, press a key to exitThe time in the header line stayed at the moment right after the run, and pressing one key ended watch. The exit code this time was 8.
echo $?8The man page describes exit code 8 as a value that includes the case where the command ended with an error.
📌 Main options: results from this article
| Option | Task | Result in this article | |
|---|---|---|---|
| -n seconds | Changes the refresh interval (default 2 seconds) | The header line changed to Every 1.0s | |
| -d | Highlights the characters that differ from the previous refresh | With the file size 7 → 14, highlighted from that column to the end of the line, gone on the next refresh | |
| -t | Turns off the header line and the empty line below it | Only one line of date output left | |
| -g | Ends watch when the output changes | Ended after running → done, exit code 0 | |
| -e | Freezes the screen when the command ends with an error | Ended after a key press, exit code 8 | |
| -v | Prints version information | watch from procps-ng 3.3.17 on the AlmaLinux 9.8 server, watch from procps-ng 4.0.2 on the Debian 12 server |
📎 Other options: per the official manual
| Option | Task | |
|---|---|---|
| -b | Beeps when the command ends with a non-zero exit code | |
| -c | Interprets ANSI color and style codes so the command output keeps its colors | |
| -p | Runs at each interval so refresh times do not drift later | |
| -x | Runs the command with exec instead of sh -c, so less quoting is needed | |
| -w | Cuts long lines instead of wrapping them to the next line | |
| --differences=permanent | Keeps highlighting every place that changed since the first run | |
| -h | Prints help |
4️⃣ watch command quotes and pipes
watch runs the command it receives with sh -c, so for a command that contains a pipe(|) or $, wrap the whole command in single quotes. Watching the number of files in the working folder with [ls | wc -l] showed the whole command, pipe included, in the header line.
watch -n 1 'ls | wc -l'Every 1.0s: ls | wc -l server1: Thu Oct 29 02:30:22 2026
6Creating one more file from another window changed the number from 6 to 7 on the next refresh.
printf 'c\n' > q3.txtEvery 1.0s: ls | wc -l server1: Thu Oct 29 02:30:24 2026
7Typing [watch -n 1 ls | wc -l] without quotes makes the shell split the pipe first, so the whole screen output of watch goes to wc. So when the command to watch has a pipe, wrap the whole command in quotes.
$ follows the same rule. Running [watch -n 1 echo $$] without quotes made the current shell turn $$ into a number first, and that number also appeared in the command in the header line.
watch -n 1 echo $$Every 1.0s: echo 3755802 server1: Thu Oct 29 02:29:37 2026
3755802Wrapping it in single quotes passes $$ through unchanged, so the header line showed echo $$ and each refresh printed the PID of the newly started sh.
watch -n 1 'echo $$'Every 1.0s: echo $$ server1: Thu Oct 29 02:29:40 2026
3756270Every 1.0s: echo $$ server1: Thu Oct 29 02:29:41 2026
3756333Checking the current shell's PID with [echo $$] gave the same number that stayed fixed in the run without quotes.
echo $$3755802For values that change slowly, such as disk usage, watch with an interval of a few seconds. du command: -s prints only the total and -h prints sizes in units such as K and M. While watching the working folder with [du -sh .], adding 8,001 bytes to a log file from another window changed 28K to 36K on the next refresh.
watch -n 2 du -sh .Every 2.0s: du -sh . server1: Thu Oct 29 02:30:34 2026
28K .printf '000d\n' 0 >> grow.logEvery 2.0s: du -sh . server1: Thu Oct 29 02:30:36 2026
36K .🔲 watch command simulator
In the simulator below, run watch and then use the [🖊 Other window] chips to change files and see for yourself how the screen changes.
🔢 FAQ & Recommended Content
System status
Linux uptime Command (Checking Server Uptime & 1, 5, 15-Minute Load Averages)
The uptime command shows, in one line, how long the server has been up and the 1-, 5- and 15-minute load averages.
Text
Linux head command & tail command
The head and tail commands print the beginning and end of a file. To follow the end of a growing log, use [tail -f].
Disk
df command
The df command prints the total size, usage and free space of each file system.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.