watch Command: Usage, -n Interval, -d Highlighting, and Exiting When Output Changes

Test environmentRHEL familyAlmaLinux 9.8procps-ng 3.3.17root accountDebian 12 · procps-ng 4.0.2 regular account
Process · job scheduling commandswatchLocation/usr/bin/watchChecked 2026-11-05
Watching command output for 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

  • ↪️ Default 2-second interval and exiting with Ctrl+C

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 date
Every 2.0s: date                                                   server1: Thu Oct 29 02:28:07 2026

Thu Oct 29 02:28:07 UTC 2026

The 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 2026

watch 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 $?
0

watch 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 -v
watch from procps-ng 3.3.17
watch -v
watch from procps-ng 4.0.2

2️⃣ watch interval and highlighting options

  • ↪️ -n interval, -d highlighting, -t to hide the header line

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 date
Every 1.0s: date                                                   server1: Thu Oct 29 02:28:15 2026

Thu Oct 29 02:28:15 UTC 2026

According 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.log
Every 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.log
printf 'line 2\n' >> size.log

When 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.log

To 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 date
Thu Oct 29 02:28:19 UTC 2026

3️⃣ watch command exit conditions and options

  • ↪️ -g for output changes and -e for errors

-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.txt
Every 1.0s: cat status.txt                                         server1: Thu Oct 29 02:29:12 2026

running

From another window, the command below changed the contents of status.txt to done.

printf 'done\n' > status.txt

watch 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.txt
Every 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 exit

The 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 $?
8

The 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

OptionTaskResult in this article
-n secondsChanges the refresh interval (default 2 seconds)The header line changed to Every 1.0s
-dHighlights the characters that differ from the previous refreshWith the file size 7 → 14, highlighted from that column to the end of the line, gone on the next refresh
-tTurns off the header line and the empty line below itOnly one line of date output left
-gEnds watch when the output changesEnded after running → done, exit code 0
-eFreezes the screen when the command ends with an errorEnded after a key press, exit code 8
-vPrints version informationwatch 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

OptionTask
-bBeeps when the command ends with a non-zero exit code
-cInterprets ANSI color and style codes so the command output keeps its colors
-pRuns at each interval so refresh times do not drift later
-xRuns the command with exec instead of sh -c, so less quoting is needed
-wCuts long lines instead of wrapping them to the next line
--differences=permanentKeeps highlighting every place that changed since the first run
-hPrints help

4️⃣ watch command quotes and pipes

  • ↪️ Wrapping in single quotes and watching disk usage

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

6

Creating one more file from another window changed the number from 6 to 7 on the next refresh.

printf 'c\n' > q3.txt
Every 1.0s: ls | wc -l                                             server1: Thu Oct 29 02:30:24 2026

7

Typing [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

3755802

Wrapping 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

3756270
Every 1.0s: echo $$                                                server1: Thu Oct 29 02:29:41 2026

3756333

Checking the current shell's PID with [echo $$] gave the same number that stayed fixed in the run without quotes.

echo $$
3755802

For 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.log
Every 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.

watch command simulatorprocps-ng 3.3.17
[root@server work]#
Runs only in your browser. On the watch screen, change files with the [Other window] chips and end with Ctrl+C.

🔢 FAQ & Recommended Content

According to the man page, if you put a number of seconds in the WATCH_INTERVAL environment variable, that value is used as the interval when -n is not given. The value follows the same rules as -n, so it cannot be shorter than 0.1 seconds.

According to the man page, watch strips the control characters in the output before drawing it. To keep the colors when watching a command that prints in color, add -c, as in [watch -c].

According to the man page, in a default run the fraction of a second in the refresh time grows a little with each refresh. Adding -p makes watch run at each interval, so that fraction stays almost the same.

ℹ️ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 9   +   1   =