Monitoring command outputs with watch

We might need to continuously watch the output of a command for a period of time in equal intervals. For example, while copying a large file, we might need to watch the growth of the file size. In order to do that, we can use the watch command to execute the du command and output repeatedly. This recipe explains how to do that.

How to do it...

The watch command can be used to monitor the output of a command on the terminal at regular intervals. The syntax of the watch command is as follows:

$ watch COMMAND

For example:

$ watch ls

Or

$ watch 'COMMANDS'

For example:

$ watch 'ls -l | grep "^d"'
# list only directories

This command will update the output at a default interval of two seconds.

We can also specify the ...

Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.