Command watcher is a file watcher that executes a command when files change.
It can be used to automatically run tests, build code, or any other command when files in a directory change.
Prerequisites: Go Installation
You'll need Go 1.23 or later. Install Go if you haven't already.
Setting up your PATH
After installing Go, ensure that $HOME/go/bin
is in your PATH:
For bash users
Add to ~/.bashrc
or ~/.bash_profile
:
export PATH="$PATH:$HOME/go/bin"
Then reload your configuration:
source ~/.bashrc
For zsh users
Add to ~/.zshrc
:
export PATH="$PATH:$HOME/go/bin"
Then reload your configuration:
source ~/.zshrc
go install github.com/tmc/watcher@latest
go run github.com/tmc/watcher@latest [arguments]
watcher [flags] [command to execute and args]
-c clear terminal before each run
-depth int
recursion depth (default 1)
-dir string
directory root to use for watching (default ".")
-ignore string
comma-separated list of glob patterns to ignore
-quiet duration
quiet period after command execution (default 800ms)
-v verbose
-wait duration
time to wait between change detection and exec (default 10ms)
- Watch directories recursively with configurable depth
- Ignore files based on glob patterns (e.g., `-ignore "*.tmp,*.log"`)
- Automatically detects and watches newly created directories
- Graceful termination on SIGINT/SIGTERM
- Configurable quiet period to avoid rapid execution
- Configurable wait time between file change and command execution
- Run tests when source files change
- Trigger build steps in development
- Refresh browsers or servers
- Auto-compile code during development
In Shell A:
$ go install github.com/tmc/watcher@latest
$ mkdir /tmp/foo; cd /tmp/foo
$ watcher -v echo "triggered"
running echo triggered
triggered
Now, In Shell B:
$ touch /tmp/foo/oi
Every time /tmp/foo changes the echo will be re-executed.
License: ISC