Useful Linux command : watch
July 13, 2015

Useful Linux command : watch

Posted on July 13, 2015  •  2 minutes  • 382 words  •  Suggest Changes


Watch runs command repeatedly, displaying its output. This allows you to watch the program output change over time.  - man page

Sounds a bit cryptically right ? lets use an example to show how fun this command is !

watch svn up

Would update SVN repo in the pwd (working directory)  every 2 seconds! One could ask how useful that really is, well, I use SVN for “syncing” my data. So unlike normal repo’s I create an commit every 5 minutes. (with typo’s every few seconds) so its easy to just let the server check if I already committed stuff. Now this is just basic usage. It has a few useful features :

Another neat trick is combining commands using && this way we can do multiple commands at once.

watch  “svn up && rm -rf cache/*”

note, the quotes are needed, else it will do the first command and when watch would stop (never) execute the second command.

Can you guess what would happen here ? Every 2 seconds an svn up and a clean of the cache 🙂 automation just got easy!_ _Now lets add differences, there is only one option, cumulative or not. Cumulative will take the first output and compare it until stopped, the ’normal’ version will check only the difference between the current and the one just gone. (diff n-1, n)

watch -d -c -n 1 “cat test && echo ’lol’ » test”

write a string (lol) every second to a file, and use watch build-in diff to show the difference.

watch -differences=cumulative -c -n 1 “cat test && echo ’lol’ » test”

A more useful function could be to check if you’re application is going to OOM (out of memory), for example using :

watch -d -n 1 free -m

While it might not be a daily used command, it can help you monitor applications better. Have fun watch’ng ! Not in the creepy way!



If you enjoyed this website, consider buying me a Dr. Pepper

Buy me a Dr PepperBuy me a Dr Pepper