Beginners Guide for Watch Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

In UNIX/Linux, β€œwatch” is a resourceful utility for monitoring updates in the specified command output (including errors) by refreshing the results every 2 seconds until it is interrupted using the β€œCtrl+c” shortcut key.

It makes it easier for you to monitor the updates in background processes, disk usage, system uptime, tracking errors, and many more.

In this article, you will learn different ways to use the watch command, with practical examples.

Tutorial Details

Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Internet RequiredNo

Syntax of the Watch Command

The watch command takes two arguments: one is the option, and the other is the command (used as an argument to the watch command).

$ watch [OPTION] [COMMAND]

Monitoring the Command Output Changes

Monitor the output changes for a specific command (with or without flags) by specifying it as an argument to the watch command.

$ watch date

The above command will monitor the changes in the date command by updating the output every 2 seconds, as shown.

Monitoring the change in command output

Setting a Custom Interval for Command Output Changes

Use the β€œ-n” flag to set a custom interval to update the command output used as an argument to the watch command.

$ watch -n 1 date

The above command will execute the date command every 1 second.

Change the time interval for watch command

Highlighting the Difference in the Command Output Changes

The β€œ-d” flag will come in handy when multiple parameters are changing in the command output by highlighting each one of them when they change, as shown.

$ watch -d date


Highlighting the command update changes

Or, pass β€œcumulative” as a value to the β€œ-d” flag if you want all the values that have ever changed to stay highlighted.

$ watch -d=cumulative date


Keep the changed value highlighted

Turning Off the Header

The β€œ-t” flag will remove the header from the watch command that contains the interval time, user-defined command, and current system time.

$ watch -t date


Removing the watch command header from the command output

Alerting When the Command Fails

The β€œ-b” flag will beep when the specified command used as an argument to the watch command returns an error or a non-zero exit status.

It requires the beep package, which can be easily installed using β€œsudo apt install beep” if it is not present.
$ watch -b date


Alert when the command return non-zero exit status

Freezing When the Command Fails

The β€œ-e” flag will freeze the monitoring when the specified command used as an argument to the watch command fails or gets the non-zero exit status, asking you to press a key to exit.

In the following example, I’ve created a file with the name β€œfile.txt” that will be monitored using the watch and ls commands; when I remove that file, the monitoring will stop and ask for a key to press for quitting.

$ watch -e ls -l file.txt


Freeze the watch command monitoring when the specified command fails

Quit Monitoring When Command Output Changes

The β€œ-g” flag will quit monitoring for the specified command used as an argument to watch command on its first output change.

$ watch -g date


Exit the command on its first update

Monitoring the Multiple Piped Commands

If you want to monitor multiple commands piped together, enclose them in single or double quotes; otherwise, only the first command will get executed.

$ watch "ls -l | tail -n10; date"


Monitoring multiple commands piped together

Exit Status

The watch command has eight exit statuses:

  • 0: Success.
  • 1: Various Failures.
  • 2: Forking the process to watch failed.
  • 3: Replacing child process stdout with a write side pipe failed.
  • 4: Command execution failed.
  • 5: Closing child process write pipe failed.
  • 7: IPC pipe creation failed.
  • 8: The specified command exited on an error.

That’s all an informed Linux user should know about the watch command.

If you have any issue or question regarding this topic, then feel free to ask it in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.