What Is the Linux watch Command, How to Use It + Examples

What Is the Linux watch Command, How to Use It + Examples

There are plenty of monitoring and troubleshooting tools available for Linux VPS. However, many are difficult to install or have a steep learning curve.

This is where the Linux watch command comes in handy. It’s a built-in Linux command that lets users execute a specific command repeatedly and display the result in a standard output on a console or terminal window.

This tutorial will explain the Linux watch command and provide several use cases.


The Linux watch command is a built-in command-line tool that runs user-defined commands at regular intervals. The command is especially helpful for users who wish to monitor changes in their command output over time. It’s most commonly used to track system uptime, memory, or disk usage. The Linux watch command will run simultaneously in the terminal until stopped by pressing CTRL+C.

By default, the Linux watch command runs the user-defined command every two seconds. However, users can easily change the interval by changing its options.

Additionally, when combined with different pipeline commands, watch lets users monitor the creation and deletion of files. It is useful when users want to troubleshoot an issue or continuously monitor the script output.

Linux watch Command Syntax

The watch command syntax looks like this:

watch [options] [your command]

For example, a watch command combined with the free command would look like this:

The terminal window output for the watch and free commands. The watch command works by running a user-specified command repeatedly

The specified command shows that the watch command clears the terminal window and runs the free command every two seconds. You can find the list of most commonly used watch command options below:

OptionExplanation
-d, –differences Displays the differences between watch command updates. If you want all changed values to stay highlighted, use the combined -d=cumulative option.
-t, –no-titleTurns off the header that displays the time interval, command, current time and date, and hostname.
-n, –intervalAllows users to specify the time interval between output updates in seconds.
-b, –beepPlays a beep whenever a command exits with an error.
-g, –chgexitExits the watch command when the user command output changes.
-e, –errexitHalts watch command updates on screen when an error occurs and exits the command after a key press.
-h, –helpDisplays the help page.
-c, –colorInterprets ANSI color and style sequences.
-x, –execForwards user command to exec.
-p, –preciseTries to run the user-defined command exactly after seconds defined by the –interval option.

Linux watch Command Examples

Check out some of the watch command use cases. Start by logging in to your virtual private server.

Using watch Command to Highlight the Differences Between Updates

watch regularly updates the specified command’s output. To view the changing output, use the –d or –differences option, as it will highlight the changes:

watch -d free

Here, the RAM usage values are highlighted in the command line:

The watch command output update. It highlights the terminal content of the free command

Using watch Command to Change Time Intervals

Users can effortlessly change the duration between outputs with the -n option followed by a digit representing the number of seconds. For example:

watch -n 5 date

This command will reflect the updated date command output after every five seconds. Remember that Linux watch can’t observe regular intervals of less than 0.1 seconds.

Using watch Command to Exit After a Single Change

It’s often useful for the watch command to exit after one change in the output. Users can achieve this with the help of the –g option. An example would look like this:

watch -g free

The –g option stops the specified command whenever there are changes in memory consumption.

Using watch Command to Hide Command Header

It’s possible to turn off the header that shows a blank line, current time, command, and interval with the help of the –t option. For example:

watch -t free
The output of watch -d option. It makes the terminal window temporarily clear

How to Use the Linux watch Command with Other Pipeline Commands

The watch command can also be helpful with other pipeline commands:

Using watch Command to Monitor Files

To view a particular file status or its changes, use the following command:

watch -d 'ls -l | fgrep newuser'

Here, the newuser is the username, and ls -l represents the files in the user directory.

Using watch Command as a Clock

To use the watch command as a clock, execute the specified command below:

watch –t date

The executed date command will display the clock and date only.

Using watch Command to View Network Status

It’s possible to use the watch command to monitor your network. For example:

watch "netstat -anp | grep -c ':22\b.*LISTEN'"

The command will display active connections on port 22.

You can also pipe the tail command to retrieve specific output. For example, the following runs netstat every two seconds and prints the last three network logs:

watch -n 2 "netstat -ant | tail -3"

Conclusion

Troubleshooting and tracking changes are important parts for any Linux user, which is where the watch command comes in.

It makes it easy for users to observe various command output changes without running them repeatedly.

In this tutorial, we’ve covered the basics of the watch command and provided some excellent use cases for it.

We hope that you found this tutorial useful. If you have any questions or suggestions, leave them in the comments section below.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Author
The Co-author

Ignas R.

Ignas takes great satisfaction in helping people tackle even the most complex technical issues. His current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house.