The watch command in Linux is a powerful utility that allows users to execute a command repeatedly at fixed intervals while displaying the output in real time. Whether you’re monitoring disk usage, system processes, or log files, this command helps automate repetitive tasks and provides live updates without manually rerunning commands.
In this guide, we’ll cover everything you need to know about the watch command in Linux, including its usage, options, and practical applications. If you prefer a step-by-step video tutorial, check out our YouTube video:
What is the watch Command in Linux?
The watch command in Linux is used to run a command at regular intervals and display the continuously updated output in the terminal. By default, it refreshes every 2 seconds, making it an essential tool for system administrators and developers.
✅ Key Benefits:
- Automates repetitive command execution
- Helps track system performance in real time
- Highlights changes in output for easy monitoring
Basic Syntax:
Now, let’s explore how to use it effectively.
Basic Usage of the watch Command in Linux
The simplest way to use watch command in Linux is:
This continuously monitors disk usage and updates the display every 2 seconds.
✅ Use Case: If you’re copying large files or performing backups, this helps track available disk space without manually running df -h
.
Customizing the Refresh Interval
To change the default interval, use the -n
option.
Example: Checking Memory Usage Every 5 Seconds
This refreshes memory statistics every 5 seconds instead of 2 seconds.
✅ Use Case: Great for detecting memory leaks or tracking RAM consumption without overwhelming your system.
Highlighting Changes in Output
By default, watch command in Linux highlights changes in output, helping you spot updates more easily.
Example: Monitoring Log File Changes
This monitors the /var/log
directory and highlights newly created or modified files.
✅ Use Case: Useful for debugging system issues by tracking log file updates.
Using watch with Pipes and Filters
You can refine monitoring output by combining watch command in Linux with pipes (|
) and filters like grep
.
Example: Tracking Apache Processes Every 3 Seconds
This only shows processes related to Apache every 3 seconds.
✅ Use Case: Ensures web servers like Apache are running without manually typing ps aux | grep apache
.
Monitoring Network Activity
When using special characters like pipes (|
), enclose the command in quotes to prevent errors.
Example: Watching Active Services on Port 80
This continuously updates a list of services listening on port 80.
✅ Use Case: Helps network administrators monitor web services and detect connectivity issues.
Stopping the watch Command
To stop watch command in Linux, press:
This exits the command like any other running process in Linux.
FAQs
1. How can I change the refresh interval for the watch command in Linux?
Use the -n
option followed by the interval in seconds. Example:
This updates disk usage stats every 10 seconds instead of the default 2 seconds.
2. Can I use watch to monitor a specific log file?
Yes! You can track log updates with:
This highlights changes to the last 10 lines of /var/log/syslog
.
3. Does the watch command work on all Linux distributions?
Yes! The watch command in Linux is available on most distributions, including Ubuntu, Rocky Linux, and CentOS.
4. How can I stop watch automatically after a certain number of updates?
By default, watch
runs indefinitely. You can use a loop to control execution:
The -t
option removes the title and can be helpful for scripting.
5. Can I save the output of watch command?
Since watch
continuously updates the screen, logging requires redirecting static output:
This appends disk usage data to disk_usage.log
every 5 seconds.
Conclusion
The watch command in Linux is an indispensable tool for real-time monitoring of system resources, log files, and network activity. Whether you’re managing disk space, memory usage, running processes, or network connections, this command saves time and effort.
For a step-by-step demonstration, check out our YouTube video on using watch command in Linux efficiently. Don’t forget to like, subscribe, and hit the bell icon for more Linux tutorials! 🚀