Home » Linux » Shell » Linux Watch Command

How to Use the watch Command in Linux, With Examples

The watch command in Linux does one thing – repeats a command and outputs the result repeatedly, letting you watch for changes. Here’s how to use it.

watch Command Syntax

The syntax for the watch command is as follows:

watch OPTIONS COMMAND

Note that:

  • OPTIONS should be a list of options from the below table, which will alter the default behavior of the watch command
  • COMMAND is the command that watch should repeatedly execute, which you will monitor the output of
  • watch will run until interrupted (So press CTRL+C to exit it)

Options

Here are the commonly used options for the watch command:

-n Specify the interval which the command should run in seconds
-d Highlight the differences between successive updates
–differences=cumulative Present a running display of all positions that have changed

You can view the full user manual for the watch command by running:

man watch

Linux Watch Command Examples

Probably the most effective example of the usefulness of the watch command is to use it with the date and free commands.

The date command returns the current date and time and then quits. When combined with the watch command, you’ll get a rolling update of the current date and time, as the watch command will repeatedly execute the date command and print its output:

watch date

You’ll see something like the below:

Linux watch command

You can see that watch is running, displaying that it executes the date command every 2 seconds (seen in the top left), along with the time watch last executed the given command. Then on the second line, we can see the output of the date command, which is refreshed every time the command is run.

The free command prints how much memory is available on your computer. Combined with the watch command, you can see a live view of your computers memory usage:

watch free

 

Linux watch command

Again, you can see that watch is executing the free command every 2 seconds, as displayed in the top left corner. The current date and time, along with the hostname, are displayed in the top right.

Below that, you can see the output of the free command, updated every 2 seconds as watch repeatedly runs the command and grabs its output.

Specifying the Interval

By default, watch will run the supplied command and print its output every 2 seconds. This can be changed using the -n option:

watch -n 5 free

The above command will do the exact thing in the previous example but execute every 5 seconds instead of every 2 seconds.

Watching for Differences

If you want to see which values in the output are changing between each execution, use the -d option:

watch -d free

Linux watch command

Characters that have changed in the output since the last run are highlighted – which can be useful for monitoring the status of system services.

If you want to see the differences in the output since the first run rather than from the last run:

watch --differences=cumulative free

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment