# The `watch` command The `watch` command is used to execute a command repeatedly at regular intervals and display the output. It's particularly useful for monitoring changes in system status, file contents, or command output over time. ## Syntax ``` watch [options] command ``` ## Options Some popular option flags include: ``` -n [seconds] Set update interval (default is 2 seconds) -d Highlight differences between updates -t Turn off header showing interval and command -b Beep if command has non-zero exit status -e Exit on error (non-zero exit status) -g Exit when output changes -c Interpret ANSI color sequences -x Pass command to shell with exec -p Precise timing mode ``` ## Examples 1. Watch system uptime every 2 seconds (default) ```bash watch uptime ``` 2. Watch disk space with custom interval ```bash watch -n 5 df -h ``` 3. Monitor memory usage with differences highlighted ```bash watch -d free -h ``` 4. Watch network connections ```bash watch -n 1 'netstat -tuln' ``` 5. Monitor specific directory contents ```bash watch 'ls -la /var/log' ``` 6. Watch CPU information ```bash watch -n 2 'cat /proc/cpuinfo | grep "cpu MHz"' ``` 7. Monitor active processes ```bash watch -d 'ps aux | head -20' ``` 8. Watch file size changes ```bash watch -n 1 'ls -lh /var/log/syslog' ``` 9. Monitor system load ```bash watch -n 3 'cat /proc/loadavg' ``` 10. Watch with precise timing ```bash watch -p -n 0.5 date ``` 11. Monitor service status ```bash watch 'systemctl status nginx' ``` 12. Watch with color support ```bash watch -c 'ls --color=always' ``` 13. Exit when output changes ```bash watch -g 'cat /tmp/status.txt' ``` 14. Watch with beep on error ```bash watch -b 'ping -c 1 google.com' ``` 15. Monitor log file size ```bash watch 'wc -l /var/log/messages' ``` 16. Watch docker containers ```bash watch 'docker ps' ``` 17. Monitor temperature sensors ```bash watch -n 2 sensors ``` 18. Watch git status ```bash watch -d 'git status --porcelain' ``` 19. Monitor bandwidth usage ```bash watch -n 1 'cat /proc/net/dev' ``` 20. Watch without header ```bash watch -t 'date' ``` ## Use Cases - System monitoring and performance analysis - Watching log files for changes - Monitoring network connectivity - Tracking file system changes - Observing process behavior - Debugging system issues - Automation and scripting - Real-time status monitoring ## Key Features - **Real-time updates**: Continuously refreshes output - **Difference highlighting**: Shows what changed between updates - **Flexible intervals**: Customize update frequency - **Exit conditions**: Can exit on changes or errors - **Header information**: Shows command and update interval ## Important Notes - Press `Ctrl+C` to exit watch - Use quotes around complex commands with pipes or redirections - The command runs in a subshell each time - Be careful with resource-intensive commands and short intervals - Screen will clear and refresh with each update - Header shows last update time and interval ## Tips - Use `-d` to easily spot changes - Combine with `grep` to filter output - Use longer intervals for less critical monitoring - Consider system load when setting very short intervals The `watch` command is an essential tool for system administrators and developers who need to monitor changes in real-time. For more details, check the manual: `man watch`