How to Use the Linux watch Command with Examples

August 11, 2021

Introduction

To repeatedly run a command or job in regular time intervals while working in Linux, you can use cron jobs or bash scripts. However, Linux also offers a more straightforward, built-in solution - the watch command.

In this tutorial, you will learn the watch command syntax, how it works, and the different things it can help you do.

How to use the Linux watch command with examples

Prerequisites

Linux watch Command Overview

The watch command is a built-in Linux utility used for running user-defined commands at regular intervals. It temporarily clears all the terminal content and displays the output of the attached command, along with the current system date and time.

By default, the watch command updates the output every two seconds. Press Ctrl+C to exit out of the command output.

The watch command is useful when you need to monitor changes in a command output over time. This includes disk usage, system uptime, or tracking errors.

Linux Watch Command Syntax

The watch command uses the following syntax:

watch [option] [command]

Where:

  • [option]: Adding an option changes the way the watch command behaves. Available options are listed below.
  • [command]: A user-defined command you want to run repeatedly.

The watch command options include:

-n, --intervalAllows you to specify the interval between output updates.
-d, --differencesHighlights the differences between output updates.
-g, --chgexitExits the watch command when the output of the user-defined command changes.
-t, --no-titleRemoves the header showing the interval, command, and current time and date.
-b, --beepPlays a sound alert (beep) if the command exits with an error.
-p, --preciseAttempts to run the command after the exact number of seconds defined by the --interval option.
-e, --errexitStops output updates on error and exits the command after a key press.
-c, --colorInterprets ANSI color and style sequences.
-x, --execPasses the user-defined command to exec, reducing the need for extra quoting.
-w, --no-linewrapTurns off line wrapping and truncates long lines instead.
-h, --helpDisplays help text and exits.
-v, --versionDisplays version information and exits.

Linux Watch Command Examples

Here are some of the ways you can use the watch command options to achieve different results:

Run Command with a Custom Interval

Set a custom interval to run a user-defined command and show the output by using the -n or --interval option:

watch -n [interval in seconds] [command]

For instance, to display the system time and date every 5 seconds, run:

watch -n 5 date
Use the -n option to set a custom interval

Note: The -n option allows you to use fractions of a second, with a minimum interval of 0.1 seconds. When entering decimals, both a period (.) and a comma (,) work for any locale.

Highlighting Changes Between Updates

Use the -d or --difference option to highlight changes between successive output updates:

watch -d [command]

For example, display the system date and time in the default 2-second interval with the changes highlighted:

watch -d date
Use the -d option to highlight changes between outputs

Pass =cumulative to the -d option if you want all the values that have ever changed to stay highlighted:

watch -d=cumulative date

Exit on Change

The -g or --chgexit option causes the watch command to exit if there is a change in the output:

watch -g [command]

As an example, adding the free command monitors your system's memory consumption and exits if the value changes:

watch -g free
Use the -g option to exit the watch command if the output changes

Hide the watch Command Header

Turn off the header containing the interval time, user-defined command, and current system time in the watch command output by using the -t or --no-title option:

watch -t [command]

Returning to the example of displaying the system date and time, this time without the header:

watch -t date
Use the -t option to display the watch command output without the header

Alert on Error

The watch command uses the beep package to play a sound alert if the output update fails due to an error. To do this, use the -b or --beep option:

watch -b [command]

Note: if you don't have the beep package installed, add it with sudo apt install beep command.

Using Complex Commands

The watch command also allows you to use more complex user-defined commands, with their own arguments and options. One way to do this is to use the backslash ('\') symbol:

watch [options] \

Using the command above brings you to the next line in the terminal, where you need to add the user-defined command. Once you hit Enter, it executes the command. For instance:

watch -n 5 \
echo "watch command example output"
Adding a complex command using the backslash symbol

Another option is to add the user-define command in single quotation marks:

watch [options] '[command]'

Using the example above, the command would be:

watch -n 5 'echo "watch command example output"'
Adding a complex command using single quotation marks

Conclusion

After reading this tutorial, you should have a better understanding of how the watch command works and what you can use it for.

For a more comprehensive overview of commands, check out our ultimate list of Linux commands.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
Linux Source Command with Examples
July 22, 2021

The Linux source command allows you to execute a list of commands from a text file or script. Learn the different ways you can use the source command in this tutorial.
Read more
man Command in Linux with Examples
March 31, 2021

The man command is a built-in Linux utility that allows users to search for any available command and displays a user manual for the specified command.
Read more
Echo Command in Linux (With Examples)
February 11, 2021

The echo command prints out a text string you provide as the output message. This tutorial covers the echo command syntax and different ways you can use it.
Read more
How to Use the Linux sleep Command with Examples
February 3, 2021

The sleep command is used when it is necessary to postpone the execution of commands on the command line or in shell scripts.
Read more