Using the "watch" command (with examples)

Using the "watch" command (with examples)

The watch command is a useful tool for monitoring the output of a command over time in a full-screen mode. It allows you to continuously observe changes in the output without having to manually execute the command repeatedly. In this article, we will explore different use cases of the watch command to illustrate its versatility and practicality.

Example 1: Monitoring files in the current directory

watch ls

Motivation: Monitoring the contents of a directory can be useful when you want to keep an eye on any changes or additions to the files within it. By using the watch command with ls, you can easily track these modifications in real-time.

Explanation: In this example, the ls command is used inside the watch command, which continuously executes ls and updates the output on the screen. This allows you to monitor the files in the current directory as they change.

Example output:

file1.txt
file2.txt
dir1/
dir2/

Example 2: Monitoring disk space and highlighting changes

watch -d df

Motivation: Keeping track of disk space usage is crucial for maintaining system health and preventing data loss. By using the -d flag with the watch command, you can easily visualize changes in disk space utilization and identify any abnormal or unexpected variations.

Explanation: The df command is used to display disk usage information on a Linux system. By running df inside the watch command with the -d flag, the output will be highlighted to emphasize any changes. This allows you to instantly notice any modifications, such as an increase or decrease in disk space.

Example output:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        1048576  512000    536576  49% /mnt/storage

Example 3: Monitoring “node” processes, refreshing every 3 seconds

watch -n 3 "ps aux | grep node"

Motivation: When working with Node.js applications, it can be helpful to monitor the running “node” processes in real-time. By employing the watch command with a periodic refresh interval, you can observe any changes in the running processes over time.

Explanation: In this example, the ps aux | grep node command is executed inside the watch command with the -n 3 flag. This runs the “ps aux | grep node” command every 3 seconds, allowing you to continuously monitor the “node” processes on your system.

Example output:

user     1234  0.0  0.4  12345  6789 ?        Ssl  00:00   0:05 node app.js

Example 4: Monitoring disk space and stopping when it changes

watch -g df

Motivation: Sometimes, you may only be interested in monitoring disk space until a change occurs. With the -g flag, you can use the watch command to keep track of disk usage and automatically stop when any modifications are detected.

Explanation: By running df command inside the watch command with the -g flag, the command will continue executing until a change in disk space is detected. Once a change occurs, the watch command will stop and display the final output.

Example output:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        1048576  512000    536576  51% /mnt/storage

Conclusion: The watch command is a powerful tool that allows you to conveniently monitor the output of a command in real-time. Whether you need to keep an eye on file changes, disk space usage, or running processes, the watch command provides you with a flexible and efficient way to stay up-to-date with the information you need.

Related Posts

How to use the command cronic (with examples)

How to use the command cronic (with examples)

The command cronic is a bash script used to wrap cron jobs and prevent excess email sending.

Read More
How to use the command `wl-paste` (with examples)

How to use the command `wl-paste` (with examples)

wl-paste is a tool used to access data stored in the clipboard for Wayland, a display protocol.

Read More
Ruby (with examples)

Ruby (with examples)

Ruby is a popular programming language known for its simplicity and readability.

Read More