When you are managing a Linux server, you frequently need to monitor the output of a specific command over time. For example, if you are downloading a massive file using a background script, you might constantly type ls -l over and over again to watch the file size grow. Repeatedly mashing the Up Arrow and the Enter key is incredibly tedious and clutters your terminal history. To force any Linux command to execute repeatedly on an automated loop and display its output cleanly on a full screen, you must use the watch command.
How the watch Command Works
The watch command acts as a wrapper. It takes the primary command you want to run, completely hijacks your terminal window to create a clean dashboard, and executes the primary command over and over again at a highly specific interval.
To continuously monitor the file size of an active download (e.g., database.tar.gz), you simply place the word watch directly in front of the ls command:
watch ls -lh database.tar.gz
Your terminal screen will instantly clear. At the absolute top-left corner of the screen, a small header will appear displaying the exact time and the specific command that is running. Below the header, the output of the ls command is displayed. By default, the watch engine will silently re-run the ls command exactly once every 2 seconds, instantly refreshing the output on your screen.
How to Change the Refresh Interval
If you are monitoring a command that consumes a massive amount of CPU power (like a complex database query), running it every 2 seconds will crash the server. You must use the -n (interval) flag to forcefully slow down the refresh rate.
To run the command exactly once every 10 seconds, type:
watch -n 10 ls -lh database.tar.gz
Conversely, if you are monitoring highly sensitive, real-time network traffic, you can drop the interval down to 0.1 seconds (the minimum limit).
How to Highlight Changes
If you are monitoring a massive block of text, it is nearly impossible for the human eye to detect if a single number changes during the 2-second refresh cycle. You can command the engine to physically highlight any character that changes between loops using the -d (differences) flag.
watch -d ls -lh database.tar.gz
As the file size increases, the specific digits representing the megabyte count will briefly flash white on the screen during the exact moment they change, drawing your eye directly to the active data.
To stop the watch loop and return to your standard terminal prompt, simply press Ctrl + C.