Skip to main content

watch Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for rerunning commands at intervals and monitoring output changes with watch in Linux

The `watch` command reruns another command at a fixed interval and refreshes the output in the terminal. This cheatsheet covers intervals, highlighting changes, command pipelines, and practical monitoring patterns.

Basic Syntax

Core watch command forms.

CommandDescription
watch commandRun a command repeatedly every 2 seconds
watch -n 5 commandRefresh every 5 seconds
watch -n 1 dateUpdate output every second
watch --helpShow available options

Common Monitoring Tasks

Use watch to keep an eye on changing system state.

CommandDescription
watch free -hMonitor memory usage
watch df -hMonitor disk space
watch uptimeCheck load averages and uptime
`watch “ps -efgrep nginx”`
watch "ss -tulpn"Monitor listening sockets

Timing and Refresh Control

Control how often watch reruns the command.

CommandDescription
watch -n 0.5 commandRefresh every 0.5 seconds
watch -n 10 commandRefresh every 10 seconds
watch -t commandHide the header line
watch -p commandTry to run at precise intervals
watch -x command arg1 arg2Run the command directly without sh -c

Highlighting Changes

Make changing output easier to spot.

CommandDescription
watch -d commandHighlight differences between updates
watch -d free -hHighlight memory changes
watch -d "ip -brief address"Highlight interface state or address changes
watch -d -n 1 "cat /proc/loadavg"Highlight load-average changes

Commands with Pipes and Quotes

Wrap pipelines and shell syntax in quotes unless you use -x.

CommandDescription
`watch “ps -efgrep apache”`
`watch “ls -lh /var/logtail”`
watch "grep -c error /var/log/syslog"Count matching lines repeatedly
watch -x ls -lh /var/logRun ls directly without shell parsing
`watch “find /tmp -maxdepth 1 -type fwc -l”`

Exit Conditions and Beeps

Stop or alert when output changes.

CommandDescription
watch -g commandExit when command output changes
watch -b commandBeep if the command exits with non-zero status
watch -b -n 5 "systemctl is-active nginx"Alert if the service is no longer active
watch -g "cat /tmp/status.txt"Exit when the file content changes

Troubleshooting

Quick fixes for common watch issues.

IssueCheck
Pipes or redirects do not workQuote the whole command or use sh -c
The screen flickers too muchIncrease the interval or hide the header with -t
Output changes are hard to spotAdd -d to highlight differences
The command exits immediatelyCheck the command syntax outside watch first
You need shell expansion and variablesUse quoted shell commands instead of -x

Use these guides for broader monitoring and process tasks.

GuideDescription
Linux Watch CommandFull guide to watch options and examples
ps Command in LinuxInspect running processes
free Command in LinuxCheck memory usage
ss Command in LinuxInspect sockets and ports
top Command in LinuxMonitor processes interactively