When you are waiting for a large file download to finish in a different terminal window, or waiting for a background script to update a log file, constantly typing ls -l or cat every five seconds is tedious and inefficient. Instead of manually running the same command over and over, you can use the Linux watch command to run it automatically on a loop.
What Does the Watch Command Do?
The watch utility takes any standard terminal command and executes it repeatedly at a set interval (usually every 2 seconds). It clears the screen between each execution, creating a live, updating dashboard of the command’s output.
Basic Usage
To use the command, simply type watch followed by the command you want to monitor.
For example, if you want to monitor the size of a specific file as it downloads, you would normally use ls -lh filename.zip. To monitor it live, run:
watch ls -lh filename.zip
Your terminal will clear, and you will see the file size updating in real-time. To exit the live dashboard and return to your normal prompt, press Ctrl + C.
Advanced Features
The watch command has several powerful flags that improve its functionality.
- Change the interval: By default, it updates every 2 seconds. You can change this using the
-nflag. To update every 5 seconds, run:watch -n 5 ls -lh. - Highlight differences: If you are monitoring a massive block of text (like
ifconfigorsensors), it is hard to see what actually changed between updates. If you use the-d(differences) flag, the terminal will highlight any text that changed since the last update in white. Example:watch -d sensors.