{"id":3495,"date":"2018-05-01T19:54:17","date_gmt":"2018-05-01T15:54:17","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-watch-command\/"},"modified":"2018-05-01T19:54:17","modified_gmt":"2018-05-01T15:54:17","slug":"linux-watch-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-watch-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux watch Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Sometimes, while working on the Linux command line, you might want to execute a command repeatedly so as to track any change in output. Well, you&#8217;ll be happy to know there exists a command line utility that lets you do this. The tool in question is <strong>Watch<\/strong>, and in this tutorial, we will discuss some of its basic features using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples used in this article have been tested on Ubuntu 16.04 LTS.<\/p>\n<h2 id=\"linux-watch-command\">Linux watch command<\/h2>\n<p>As already mentioned, the watch utility\u00a0lets you execute a command repeatedly. Following is the syntax of this command:<\/p>\n<p class=\"command\">watch [options] command<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page describes it:<\/p>\n<pre>watch runs command repeatedly, displaying its output and errors (the<br\/>first screenfull). This allows you to watch the program output change<br\/>over time. By default, the program is run every 2 seconds. By<br\/>default, watch will run until interrupted.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the &#8216;watch&#8217; command works.<\/p>\n<h2 id=\"q-how-watch-command-works\">Q1. How watch command works?<\/h2>\n<p>The baisc usage of watch is very easy &#8211; all you have to do is to run the tool with a command name as input. For example:<\/p>\n<p class=\"command\">watch date<\/p>\n<p>The above command produced the following output on my system:<\/p>\n<p><a class=\"fancybox\" id=\"img-watch-default\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/watch-default.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples.png\" alt=\"How watch command works?\" width=\"500\" height=\"48\" title=\"\"><\/a><\/p>\n<p>The first line of the output tells the output of the command &#8216;date&#8217; is being refreshed after every 2 seconds (that&#8217;s the default time interval), followed by the current date\/time of the system. The second line is the actual output of the &#8216;date&#8217; command that&#8217;s getting refreshed after every couple of seconds.<\/p>\n<h2 id=\"q-how-to-make-watch-highlight-difference-between-updates\">Q2. How to make watch highlight difference between updates?<\/h2>\n<p>As already explained in the previous section, the output of the command being tracked is regularly updated (after 2 seconds by default). In case you want, you can also make &#8216;watch&#8217; highlight the part of output that&#8217;s changing between updates.<\/p>\n<p>This you can do using the -d command line option. For example:<\/p>\n<p class=\"command\">watch -d date<\/p>\n<p>And here&#8217;s how the watch command highlights the changes now:<\/p>\n<p><a class=\"fancybox\" id=\"img-watch-d\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/watch-d.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to make watch highlight difference between updates?\" width=\"500\" height=\"43\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-customize-update-interval\">Q3. How to customize update interval?<\/h2>\n<p>As already mentioned, the default update interval is 2 seconds. However, if you want, you can change this value by using the -n command line option, which requires you to pass the new interval value as its input.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">watch -n 5 date<\/p>\n<p>This command will make sure the output gets updated after every 5 seconds. Please note that intervals less that 0.1 seconds aren&#8217;t allowed.<\/p>\n<p><strong>Note<\/strong>: To make sure the interval gap is honored very precisely, use the -p command line option. Here&#8217;s how the man page describes this option:<\/p>\n<pre>Make watch attempt to run command every interval seconds. Try it<br\/>with ntptime and notice how the fractional seconds stays<br\/>nearly) the same, as opposed to normal mode where they continu?<br\/>ously increase.<\/pre>\n<h2 id=\"q-how-to-turn-off-the-header-in-output\">Q4. How to turn off the header in output?<\/h2>\n<p>If you want, you can also ask &#8216;watch&#8217; to turn off the header\u00a0showing the interval, command, and current<br \/>time at the top of the display, as well as the following blank\u00a0line. This can be done using the -t command line option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">watch -t date<\/p>\n<p>Here&#8217;s the output produced by the command above:<\/p>\n<p><a class=\"fancybox\" id=\"img-watch-t\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/watch-t.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-2.png\" alt=\"How to turn off the header in output?\" width=\"500\" height=\"72\" title=\"\"><\/a><\/p>\n<p>So you can see the header\u00a0was completely stripped off.<\/p>\n<h2 id=\"q-how-to-make-watch-exit-when-the-output-changes\">Q5. How to make watch exit when the output changes?<\/h2>\n<p>If you want the &#8216;watch&#8217; command to exit after a change in output is detected, use the -g command line option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">watch -g\u00a0free<\/p>\n<p>So anytime the memory consumption in the system changes, the watch command will exit.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As you&#8217;d have understood by now, watch is an excellent way to keep track of changes, be it on system level, directory level, or even file level. Here, in this tutorial, we have discussed several key command line options of &#8216;watch.&#8217; Once you are done practicing these, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/watch\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>\u00a0to learn more.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-watch-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-watch-command%2F&amp;text=Linux+watch+Command+Tutorial+for+Beginners+%285+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-watch-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-watch-command-tutorial-for-beginners-5-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the Linux command line, you might want to execute a command repeatedly so as to track any change in output. Well, you&#8217;ll be happy to know there exists a command line utility that lets you do this. The tool in question is Watch, and in this tutorial, we will discuss some &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-3495","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=3495"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3495\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}