The Linux watch command is a power user favorite because it allows them to execute a specific command periodically and display the result in a standard output on a console or terminal window. Today we’ll show you how to start using this powerful utility on your VPS or Unix based machine!
What is the Linux Watch Command
With Linux watch, we can track the changes in the output from time to time. It is beneficial for reflecting the real-time view of events that are happening on an operating system.
By default, it runs the program every two seconds, although using –n or –interval options enables specifying a different frame.
Watch also offers its users the ability to monitor the creation and deletion of files, if you use it with various pipeline commands.
Watch will run simultaneously in the terminal until stopped by pressing CTRL+C.
Linux watch is also useful when you need to troubleshoot an issue, or monitor script output continuously. The watch command comes installed, by default in all Linux distributions with a wide range of features for its users.
Basic Linux Watch Commands
Before we start learning how to use Watch, lets log into our VPS by using SSH. Check out our PuTTY tutorial if you’re encountering issues.
To use the basic features of watch, type in the following command in the command line, while replacing file_name with your preferred file:
Or if we want to see the current date, we can use this command:
Below, we could easily observe that the date is changing every two seconds followed by the current date and time of the system.
Many times we want the Watch command to exit after one change in the output. We can do this with the help of the –g option which stops the Watch command any time there are changes in memory consumption. The command would look like this:
watch –g free
It is also possible to turn off the header showing the blank line, current time, command and interval. With the help of the –t option, you can stop the header in the terminal. For example:
watch –t date
To observe bandwidth statistics, you can use the ifconfig option with Linux Watch. It would look like this:
Now you can see the RX and TX byte traffic along with the IP address of your system. It shows the network speed every two seconds, as well.
How to Highlight Differences Between Updates with Linux Watch
Watch regularly updates the output of a specified command, by default after 2 seconds. To view the changing output we can use the –d or –difference option. This option will highlight the changes. The command to highlight differences between updates is:
watch –d date
Here, the changes in the current time are highlighted in the terminal.
How to Change Time Intervals in Linux Watch?
The duration between outputs can easily change in the Linux terminal by using the -n option followed by a digit which represents the number of seconds which you want. It’s essentially changing the update intervals, or successive update time.
watch –n 5 date
This command will reflect the output updated after every 5 seconds.
When using Linux watch we can’t observe intervals less than 0.1 seconds.
How to Use the Linux Watch Command with Other Pipelines
Watch can also be helpful with other pipeline commands:
How to Monitor Files with Linux Watch
To view a particular file’s status or changes made to it, use the below command in Linux terminal:
watch -d 'ls -l | fgrep devisers'
Here, devisers is the username and ls –l are the files present in the user directory.
How to Use the Linux Watch Command as a Clock
To use watch as a clock execute the command below in the Linux terminal command like:
watch –t date
How to View Hardware Status using Linux Watch
We’re adding this one as a neat little bonus for Linux users that might not be using a VPS, as this might not work on your server. Besides, Hostinger takes care of the back end, so you don’t have to!
In this example, we’ll monitor the CPU temperature of a Linux based machine:
watch -n 1 sensors
It will display the output of the command and you’ll be able to see the temperature status of your computer!
Recent developments made to the Linux Watch command have improved its features, making it a simpler, but effective utility for accurately tracking changes in files, directories, the machine and much more! As you can tell, this is just the tip of the iceberg of what you can do with Linux Watch.
Every Linux administrator should know how to use watch for monitoring and troubleshooting purposes. It makes it easy for the user to observe various changes in the input without issuing the same command repeatedly.
We hope you add Watch to your arsenal as well! Happy developing!