Watch command in Linux with Examples

Have you ever had to sit at a Linux terminal and repeatedly type the same command while waiting for a different result? A common example from personal experience would be typing the ls command when waiting for a certain file to appear in a directory, such as when a running Bash script is expected to generate a file. For a situation like that is exactly why we have the watch command in Linux.

watch command will continually run a specified command, refreshing your screen with the updated output so you can monitor it for the desired change you’ve been waiting on. As you can imagine, this is a lot more convenient than constantly hitting the up arrow key and Enter over and over again.

It doesn’t matter if you need to run the same command every second, 10 seconds, or 10 minutes. It’s very easy to specify a time interval with the watch command. You can also configure it to highlight changed output on your screen, so you’ll know exactly when you’ve achieved the result you’re waiting on. In this guide, we’ll show you all of these mentioned options plus a few more. Are you ready to master the watch command and reduce your command line spam? You’re about to find out how!

In this tutorial you will learn:

  • How to use the watch command on Linux
Watch command in Linux with Examples
Watch command in Linux with Examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software watch
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The watch command accepts another command and its options as input. It will then continually run that command at a specified interval. Take a look at some of the examples below to see how it works.

Watch command in Linux Basic Examples

  1. By default, the watch command will run your specified command every 2 seconds, while continuing to display the latest output in your terminal. For example, this command would run ls -l every 2 seconds.
    $ watch ls -l
    

    The output in the top left of your terminal shows what command watch is running, as well as how often it runs it (every 2 seconds in this case). The top right shows the date, right down to the seconds, so even if the output of ls -l hasn’t changed, you still know that watch is refreshing your screen because the date continues to change. To exit watch at any time, use the Ctrl + C keyboard combo.

    Running the ls -l command every 2 seconds via watch
    Running the ls -l command every 2 seconds via watch
  2. You can specify a different time interval to run the command with the -n or --interval option. You will need to specify the amount of time in seconds. For example, the following command would run ls -l every 0.5 seconds.
    $ watch -n 0.5 ls -l
    

    Running the ls -l command every half a second via watch
    Running the ls -l command every half a second via watch
  3. Use the -d or --differences option if you want watch command in Linux to highlight the changes that occur between each refresh. This can be a lot easier than manually scanning for differences every time there’s an update. You may also use the -d=permanent option if you want the highlighting to remain permanent in successive refreshes.
    $ watch -d date
    

    We are watching the date command in this example just to illustrate how the changes get highlighted. In the screenshot below, notice how watch has highlighted three characters that have changed. That’s because the clock has turned to a different minute, and the date output has reflected that in its output.

    Using the --differences option to highlight changes in the watch command
    Using the –differences option to highlight changes in the watch command
  4. Maybe instead of highlighting the changes, you’d prefer watch to completely exit when a change is detected. This can be achieved by putting the -g or --chgexit option in your command.
    $ watch -g ls -l
    
NOTE
You can always use the man command to read more about the watch command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The commands above covered some of the most frequently used options for the watch command in Linux, but there are even more options available. Check the following examples to see some of the more advanced usage of the watch and some options that you may not see used very often.

  1. If you don’t like seeing the text at the top of the watch screen, where it shows what command is running and at what interval, you can get rid of it with the -t or --no-title option.
    $ watch -t date
    
  2. You can string multiple commands together by putting them inside quotes.
    $ watch -t 'date && ls -l'
    

    Running two commands with the watch command, and achieving a cleaner output with the -t option
    Running two commands with the watch command, and achieving a cleaner output with the -t option
  3. The -p or --precise option will allow watch to be very precise by synchronizing its updates with official time servers.
    $ watch -p ls -l
    
  4. Use the -b or --beep option to make your system emit a beeping sound when a command exits with a non-zero exit.
    $ watch -b tail /var/logs/apache.log
    
  5. Use the -e or --errexit option to make the watch screen stop updating when the command encounters an error. This will ensure that you don’t miss a potential error on your screen by having it refreshed and overwritten at the specified interval.
    $ watch -e tail /var/logs/apache.log
    

Closing Thoughts

In this guide, we learned all about the watch command on Linux. The watch command is all about providing convenience and saving our wrists from carpal tunnel. Rather than spamming the same command over and over, watch does it for us. The options covered in this guide will help you get even more out of this handy utility.



Comments and Discussions
Linux Forum