watch (with examples)

watch (with examples)

The watch command is a useful tool for executing a program periodically and displaying its output in fullscreen. It is commonly used for monitoring the output of a command, tracking changes in a directory, or repeatedly running a pipeline.

In this article, we will explore eight different use cases of the watch command, demonstrating its versatility and usefulness in various scenarios.

Repeatedly running a command and showing the result

Code:

watch ls

Motivation: This use case is helpful when you want to continuously monitor the contents of a directory without manually running the ls command repeatedly. With watch, the command is automatically executed at regular intervals, providing live updates.

Explanation: The watch command takes a single argument, which is the command to be executed periodically. In this example, the ls command is used to list the files and directories in the current directory. By default, watch updates every 2 seconds.

Example Output:

Every 2.0s: ls                                                

file1.txt   file2.txt   directory1/

Re-running a command every 60 seconds

Code:

watch -n 60 date

Motivation: This use case is useful when you need to monitor the system time in real-time. By specifying the interval of 60 seconds with -n, the date command will be executed every minute, giving you updated information without continuously typing the command.

Explanation: The -n option of the watch command allows you to specify the interval in seconds. In this example, the date command is executed every 60 seconds, providing the current system date and time.

Example Output:

Every 60.0s: date                                              
Sun Oct 10 18:24:08 UTC 2021

Monitoring the contents of a directory, highlighting differences as they appear

Code:

watch -d ls -l

Motivation: This use case is particularly useful when you want to monitor changes in a directory, such as new files being created or existing files being modified or deleted. With the -d option, watch highlights the differences between consecutive invocations of the command, making it easy to identify any changes.

Explanation: The -d option of the watch command enables the highlighting of differences between subsequent executions of the command. In this example, the ls -l command is used to list the contents of the current directory with detailed information. Any changes in the directory will be clearly marked for easy identification.

Example Output:

Every 2.0s: ls -l                                              
total 8
-rw-r--r-- 1 user user    0 Oct 10 18:27 file1.txt
-rw-r--r-- 1 user user    0 Oct 10 18:27 file2.txt
drwxr-xr-x 2 user user 4096 Oct 10 18:27 directory1/

Repeatedly running a pipeline and showing the result

Code:

watch 'echo "Hello" | sed "s/Hello/Hi/"'

Motivation: This use case is handy when you want to continuously process the output of a command using a pipeline and see the modified result. By wrapping the pipeline in quotes and executing it with watch, you can quickly observe the changes made by each pipeline component.

Explanation: The watch command can also execute a pipeline by enclosing it in single quotes. In this example, the echo command outputs “Hello”, which is then piped to sed to replace “Hello” with “Hi”. The result is displayed in real-time thanks to watch.

Example Output:

Every 2.0s: echo "Hello" | sed "s/Hello/Hi/"                     
Hi

By exploring these different use cases of the watch command, you can leverage its capabilities to monitor output, track changes in a directory, and continuously process data in real-time. Whether you need to keep an eye on a command’s output or monitor a system’s status, watch can be a valuable tool in your toolkit.

Related Posts

An Overview of Filesystem Disk Space Usage with dfc (with examples)

An Overview of Filesystem Disk Space Usage with dfc (with examples)

Introduction When managing a computer system, it is crucial to monitor and understand the disk space usage.

Read More
Using PowerShell Invoke-WebRequest (with examples)

Using PowerShell Invoke-WebRequest (with examples)

PowerShell’s Invoke-WebRequest command is a powerful tool for performing HTTP/HTTPS requests to the web.

Read More
How to Use the tart Command (with examples)

How to Use the tart Command (with examples)

The tart command is a powerful tool for building, running, and managing macOS and Linux virtual machines (VMs) on Apple Silicon.

Read More