With a long-running command, perhaps running in the background, it’s sometimes helpful to keep an eye on progress. For this, the
watch command can be useful.
watch takes a command as an argument, and every two seconds (by default) it clears the screen, prints a header line that includes the command and the current date and time, and prints the output of the command.
A Simple Example
Let’s say we’re copying all files from one partition to another. We know that the
df command will show how full each partition is, so the command:
$ watch df
…will show us both how full the source partition is (presumably this won’t change), and how full the destination partition is, and thus we can see what progress is being made.
A Little More Complex
Let’s keep an eye on memory usage:
$ watch -d -n 10 free -m
This will run the
free -m command every 10 seconds (
-n 10), and any differences in the output changes it will be highlighted for ten seconds (
You can run piped commands as an argument to
watch, but the argument must be single-quoted. The following will show the ten newest files in a directory:
$ watch 'ls -lt | head'
Some Linux administrators cobble together quick
bash scripts to keep an eye on the output of a command, but
watch does that more efficiently. A good tool for the toolbox.