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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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
- 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 -levery 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 -lhasn’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 + Ckeyboard combo.
- You can specify a different time interval to run the command with the
--intervaloption. You will need to specify the amount of time in seconds. For example, the following command would run
ls -levery 0.5 seconds.
$ watch -n 0.5 ls -l
- Use the
--differencesoption 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=permanentoption if you want the highlighting to remain permanent in successive refreshes.
$ watch -d date
We are watching the
datecommand 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
dateoutput has reflected that in its output.
- 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
--chgexitoption in your command.
$ watch -g ls -l
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.
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.
- 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
$ watch -t date
- You can string multiple commands together by putting them inside quotes.
$ watch -t 'date && ls -l'
--preciseoption will allow watch to be very precise by synchronizing its updates with official time servers.
$ watch -p ls -l
- Use the
--beepoption to make your system emit a beeping sound when a command exits with a non-zero exit.
$ watch -b tail /var/logs/apache.log
- Use the
--errexitoption 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
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.