Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

watch Command Guide

watch runs a command repeatedly and displays its output full-screen, highlighting what changed between runs. It’s the fastest way to turn any one-shot command into a live monitor.

What is watch, and Why Use It?

Imagine you start a long backup and want to see the output directory grow. Or you’re tweaking a config file and want to see the moment a service picks up the change. Typing the same command over and over is tedious — watch does it for you, every N seconds, and it clears the screen and highlights differences so changes pop out at you.

It’s perfect for polling anything that updates: disk usage, log files, process lists, network connections, or the output of a script you’re iterating on.

# Check disk usage every 2 seconds
watch df -h

# Watch a log file grow
watch -d tail -n 20 /var/log/syslog

# Watch free memory change every second
watch -n 1 free -h

# Watch a file's contents, highlighting changes
watch -d cat /tmp/myfile.txt

Key Points

  • watch re-runs the whole command line every interval (default: 2 seconds).
  • The screen is cleared and redrawn each time; use -d to highlight what changed between runs.
  • It’s a foreground program — press q (or Ctrl+C) to quit.
  • Anything that works in a shell works inside watch, including pipes.

Running a Command Every N Seconds

The -n flag sets the interval in seconds (it can be fractional):

# Every 5 seconds (the default is 2)
watch -n 5 df -h

# Every 0.5 seconds — fast polling for something that changes quickly
watch -n 0.5 ls -la /tmp

# Every 10 seconds
watch -n 10 uptime

If the command itself is slow, watch will run it as fast as it can while respecting the interval — it never overlaps two runs of the same command.

Highlighting Changes with -d

The -d (or --differences) flag highlights the parts of the output that changed since the previous run. This is huge for spotting movement at a glance:

# See exactly which lines of disk usage changed
watch -d df -h

# Highlight changes in a directory listing
watch -d -n 1 ls -l

# Combine with tail to watch a live-ish log
watch -d tail -n 10 /var/log/auth.log

Changed fields are highlighted in inverse video (usually a bright block), so you can watch a number climb without reading every digit.

Watching System Resources

watch shines when paired with the monitoring commands from elsewhere in this wiki:

# Memory over time
watch -n 1 free -h

# Top processes by CPU (a poor man's top)
watch -n 2 'ps aux --sort=-%cpu | head -n 10'

# Network connections
watch -n 2 'ss -tunap | head -n 20'

# Load average and uptime
watch -n 5 uptime

# Who is logged in
watch -n 30 who

For anything heavier, use the dedicated tools instead — top, htop, vmstat, or iostat. watch is for quick, ad-hoc polling of any command.

Passing Complex Commands

Because watch takes a single command line, wrap anything with pipes or quoting in single quotes:

# Watch the largest files in a directory
watch -d 'du -sh * | sort -rh | head -n 5'

# Watch journal entries as they arrive
watch -d 'journalctl -n 5 --no-pager'

# Watch a specific file being written to
watch -d 'tail -c 500 /var/log/nginx/error.log'

Use -- before commands that take their own flags that watch might swallow, though usually quoting is enough:

watch -n 1 -- date

Forcing Color and Precision

Some commands lose their colors when their output is not a terminal. watch has a -c flag to interpret colors properly:

# Keep dmesg colors visible
watch -c 'dmesg --color=always | tail -n 20'

And -t (no title bar) gives you a bit more room:

watch -t -n 1 'ps aux | wc -l'

Common Pitfalls

  • Forgetting to quit. watch takes over the terminal; press q or Ctrl+C to get back. If you background it by accident, use jobs and fg to find it.
  • Quoting mistakes. watch df -h | grep Filesystem runs watch df -h and pipes watch itself into grep. Wrap the whole thing: watch 'df -h | grep Filesystem'.
  • Very short intervals. -n 0.1 on a slow command just makes it run back to back; the interval is a floor, not a guarantee.
  • Colors disappearing. Use -c for commands that emit ANSI colors conditionally.
  • It’s not a scheduler. For running things on a schedule, look at cron or systemd timers — watch only lives as long as your terminal session.
  • top — the interactive process monitor
  • htop — top’s improved successor
  • free — memory usage, often watched
  • vmstat — virtual memory statistics
  • iostat — CPU and I/O statistics
  • uptime — load averages, watchable
  • journalctl — logs worth polling
  • time — measure how long something takes

Cheatsheet

CommandWhat it does
watch df -hRun df -h every 2 seconds, full-screen
watch -n 1 free -hRun every 1 second
watch -d ls -lHighlight what changed between runs
watch -n 5 uptimeEvery 5 seconds
watch 'ps aux --sort=-%cpu | head'Watch the busiest processes
watch -c 'dmesg | tail -n 20'Preserve ANSI colors
watch -t -n 1 dateNo title bar, every second
watch -n 0.5 ls /tmpFast polling (fractional seconds OK)
qQuit watch