The Linux watch command lets you automate these tasks without having to manually run them again and again.

Lets see how to use the watch command in Linux.

What Is the Watch Command?

A man sitting in front of an open laptop, with a relaxed expression and his hands resting on the back of his head.

This lets you observe the changes in the output in real time.

It refreshes the output at each interval, overwriting the command’s previous output.

This means you could tell the watch command to wait for x seconds before repeating the command.

watch output of ls command

To change the update interval, use the-noption followed by the time interval in seconds.

Using the-doption, you could also highlight the changes between the previous and updated output.

By convention, the value is 0 on success and non-zero to represent an error.

change update interval of watch command

The watch commands-boption plays a beep sound if the command returns a non-zero exit.

The beep will stop when the service resumes.

To test this, open another terminal window and stop the service.

This will give constant beeps until you start the service again.

However, to use this feature, you must have the beep package installed on your system.

you’re free to do this using the-goption.

This option is very useful in scenarios when you are waiting for certain changes in the output.

Once the output changes, the watch command will stop.

you’re free to combine this usage with the echo command to display a message on the screen.

For example, consider a scenario where you are waiting for a file to arrive in your directory.

The following command will monitor the directory for the specified file.

Once the user logs in, the watch command will stop and display a message on the terminal.

It saves you from frequently typing the commands to see what has changed.