Skip to content

Linux watch command

The Linux watch command is a handy tool that allows users to monitor the output of a specified command at regular intervals. It continuously executes the command and updates the display with the most recent output. This can be useful for keeping an eye on changing data, such as log files, system performance metrics, or program output. By default, watch refreshes every 2 seconds, but this interval can be customized. It provides a convenient way to observe and track real-time updates without the need for manual intervention. The watch command is a simple yet powerful utility for monitoring processes and staying informed of changes as they occur.

watch Syntax:

Terminal window
watch [option] [command]

Options:

OptionDescription
-nSpecify the interval between command executions
-tTurn off watch’s command line timestamp
-dHighlight the difference between successive updates
-hDisplay help information

Parameters:

ParameterDescription
commandThe command to be watched

watch Usage:

Monitor System Time

Terminal window
watch date

This command will continuously display the system time.

Monitor Disk Usage

Terminal window
watch df -h

Constantly monitors disk space usage on the system.

Monitor Log File Changes

Terminal window
watch tail /var/log/syslog

Shows updates on the syslog file, displaying the last few lines.

Monitor CPU Usage

Terminal window
watch -n 1 mpstat

Continuously displays CPU usage information refreshed every 1 second.

How do I use watch in Linux?

To use the watch command in Linux, execute the following command:

Terminal window
watch ls

How can I customize the refresh interval in watch?

To customize the refresh interval in the watch command, specify the interval using the -n or —interval option followed by the number of seconds. For example:

Terminal window
watch -n 5 ls -l

How can I make watch highlight the changes in the output?

To make watch highlight the changes in the output, you can use the —differences or -d option. This will highlight the changes since the last refresh. For example:

Terminal window
watch --differences ls

How do I stop watch from clearing the screen before each refresh?

To prevent watch from clearing the screen before each refresh, you can use the -c or —no-clear option. This will keep the previous output on the screen. For example:

Terminal window
watch --no-clear date

How can I display the elapsed time and date alongside the command output in watch?

To display the elapsed time and date alongside the command output in watch, you can use the -t or —timestamp option. This will show the current time and date on each line of output. For example:

Terminal window
watch --timestamp ps aux

How can I run a command with watch only a specific number of times?

To run a command with watch only a specific number of times, you can use the -c or —count option followed by the number of times you want the command to run. For example, the following command will run ls 5 times:

Terminal window
watch --count=5 ls

How can I have watch display the output in color?

To make watch display the output in color, you can use the —color or -c option. This will enable color highlighting in the output. For example:

Terminal window
watch --color ls --color

Applications of the watch command

  • Monitoring system processes
  • Checking the output of a command periodically
  • Monitoring system resources
  • Checking log file updates in real-time
  • Monitoring system performance metrics