Watch Command in Linux [with Examples]

In this guide, we will discuss some of the basic features of the watch command using easy to understand examples.

Sometimes, while working on the Linux command line, you might want to execute a command repeatedly to track any output change. Luckily, there is a command-line utility that lets you do this.

With the Linux watch command, you can track the changes in the output from time to time. It is beneficial for reflecting the real-time view of events on an operating system.

The watch command comes installed, by default, on nearly all Linux distributions. It is useful when monitoring changes in a command output over time. So instead of reading the whole output, you can keep an eye on the changes.

How to Use watch Command in Linux

The syntax for the watch command is as follows:

watch [OPTIONS] COMMANDCode language: CSS (css)

Where:

  • [OPTIONS]: Adding an option changes how the watch command behaves.
  • [COMMAND]: A user-defined command you want to run repeatedly.

By default, the specified command will run every 2 seconds, and the watch command will run until interrupted by the user (pressing Ctrl+C).

For example, we can monitor the output of the date command every two seconds:

watch date
watch Command in Linux

As a result, the watch command will temporarily clear all of the terminal content and start running the command at regular intervals. You can see the update interval and the executed command on the top left side of the screen header.

How to Change the Time Interval

We know that by default, the command runs every 2 seconds. If we pass the -n (--interval) option to the watch command, we can specify the update interval. You will need to set the amount of time in seconds.

I’ll continue to use the date command as an example. The following command will run the date command every second.

watch -n 1 date
Changing the Time Interval on the watch Command

Highlighting the Difference

If you’re running a command that prints out a big output, keeping an eye for changes can become pretty troublesome. Fortunately, the watch command can highlight the difference between the previous and current output.

We can use the -d (--difference) option to view the changing output. This option will highlight the changes.

watch -n 1 -d date
Highlighting the Differences with watch Command

However, if the interval between the updates is very short, for example, -n 0.1, it will be challenging to review differences. Therefore, you must set a reasonable update interval.

Hide Header in Output

The -t (--no-title) option is used to turn off the header showing the interval, command, and the current time at the top of the display if you’re not interested in seeing this portion.

watch -t date
Turn off Headers

Exiting When Change Occurs

By default, the watch command keeps running until it is interrupted manually by the user (Ctrl+C). However, sometimes instead of highlighting the changes, you’d prefer the watch command to exit when a change is detected completely.

You can set watch to exit when the output from the command changes by using the -g (--chgexit) option.

watch -g date

The above example stops the watch command any time there are changes in the date command output, which in this case means after 2 seconds.

Beep on Error

The watch command can also give a beep sound if an update fails. It uses the beep package to play a sound alert if the output update fails due to an error.

watch -b incorrect-command

Conclusion

Now you know all about the watch command on Linux. Although it’s a simple program, it can be helpful if you use it properly.

Please find out more about various options in watch on its command line manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%