file-watcher
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFile Watcher Skill
File Watcher Skill
Monitor files and directories for changes and trigger actions when changes occur.
监控文件和目录的变更,在变更发生时触发操作。
When to Use
适用场景
✅ USE this skill when:
- "Watch for file changes"
- "Run command when file changes"
- "Monitor directory for new files"
- "Execute script on file create"
- "Watch multiple files for changes"
✅ 在以下场景使用本skill:
- "监控文件变更"
- "文件变更时运行命令"
- "监控目录中的新文件"
- "文件创建时执行脚本"
- "监控多个文件的变更"
When NOT to Use
不适用场景
❌ DON'T use this skill when:
- One-time file check → use test/read commands
- Scheduled monitoring → use cron
- Need persistent logging → use dedicated monitoring
❌ 请勿在以下场景使用本skill:
- 一次性文件检查 → 使用测试/读取命令
- 定时监控 → 使用cron
- 需要持久化日志 → 使用专用监控工具
Commands
命令
Watch Directory
监控目录
bash
{baseDir}/watch.sh <directory> --command "echo 'Changed'"
{baseDir}/watch.sh ./src --command "npm run build"
{baseDir}/watch.sh ./logs --command "./process.sh" --pattern "*.log"bash
{baseDir}/watch.sh <directory> --command "echo 'Changed'"
{baseDir}/watch.sh ./src --command "npm run build"
{baseDir}/watch.sh ./logs --command "./process.sh" --pattern "*.log"Watch Single File
监控单个文件
bash
{baseDir}/watch.sh --file config.json --command "echo 'Config changed'"
{baseDir}/watch.sh --file /var/log/app.log --command "tail -5 /var/log/app.log"bash
{baseDir}/watch.sh --file config.json --command "echo 'Config changed'"
{baseDir}/watch.sh --file /var/log/app.log --command "tail -5 /var/log/app.log"Watch Multiple Files
监控多个文件
bash
{baseDir}/watch.sh --files "file1.txt,file2.txt,file3.txt" --command "echo 'Changed'"
{baseDir}/watch.sh --files-from list.txt --command "echo 'Changed'"bash
{baseDir}/watch.sh --files "file1.txt,file2.txt,file3.txt" --command "echo 'Changed'"
{baseDir}/watch.sh --files-from list.txt --command "echo 'Changed'"Event Types
事件类型
bash
{baseDir}/watch.sh ./src --command "make" --events create,modify,delete
{baseDir}/watch.sh ./src --command "make" --events create
{baseDir}/watch.sh ./src --command "make" --events modifybash
{baseDir}/watch.sh ./src --command "make" --events create,modify,delete
{baseDir}/watch.sh ./src --command "make" --events create
{baseDir}/watch.sh ./src --command "make" --events modifyBackground Mode
后台模式
bash
{baseDir}/watch.sh ./src --command "make" --daemon
{baseDir}/watch.sh ./src --command "make" --pidfile /tmp/watcher.pidbash
{baseDir}/watch.sh ./src --command "make" --daemon
{baseDir}/watch.sh ./src --command "make" --pidfile /tmp/watcher.pidDebounce
防抖机制
bash
{baseDir}/watch.sh ./src --command "npm run build" --debounce 2
{baseDir}/watch.sh ./src --command "npm run build" --debounce-filebash
{baseDir}/watch.sh ./src --command "npm run build" --debounce 2
{baseDir}/watch.sh ./src --command "npm run build" --debounce-fileOptions
选项
| Option | Description | Default |
|---|---|---|
| Command to run on change | Required |
| Watch single file | None |
| Watch multiple files | None |
| Read file list from file | None |
| File pattern to match | |
| Events: create,modify,delete | all |
| Watch subdirectories | true |
| Don't recurse | false |
| Debounce time | 0 |
| Debounce per file | false |
| Run in background | false |
| PID file for daemon | None |
| Stop after N seconds | None |
| Suppress output | false |
| Show all events | false |
| 选项 | 描述 | 默认值 |
|---|---|---|
| 变更时执行的命令 | 必填 |
| 监控单个文件 | 无 |
| 监控多个文件 | 无 |
| 从文件中读取待监控文件列表 | 无 |
| 匹配的文件模式 | |
| 事件类型:create,modify,delete | 全部 |
| 监控子目录 | true |
| 不递归监控 | false |
| 防抖时间 | 0 |
| 按文件单独防抖 | false |
| 在后台运行 | false |
| 后台进程的PID文件 | 无 |
| N秒后停止运行 | 无 |
| 抑制输出 | false |
| 显示所有事件 | false |
Events
事件类型
| Event | Description |
|---|---|
| File or directory created |
| File content modified |
| File or directory deleted |
| File moved (renamed) |
| File accessed (read) |
| File attributes changed |
| 事件 | 描述 |
|---|---|
| 文件或目录被创建 |
| 文件内容被修改 |
| 文件或目录被删除 |
| 文件被移动(重命名) |
| 文件被访问(读取) |
| 文件属性被修改 |
Output Format
输出格式
[2026-02-25 20:41:00] MODIFY /path/to/file.txt
Running: echo 'Changed'
...
[2026-02-25 20:41:01] CREATE /path/to/newfile.txt
Running: echo 'Changed'
...With :
--jsonjson
{"event": "MODIFY", "file": "/path/to/file.txt", "timestamp": "2026-02-25T20:41:00Z", "command_output": "..."}[2026-02-25 20:41:00] MODIFY /path/to/file.txt
Running: echo 'Changed'
...
[2026-02-25 20:41:01] CREATE /path/to/newfile.txt
Running: echo 'Changed'
...使用选项时:
--jsonjson
{"event": "MODIFY", "file": "/path/to/file.txt", "timestamp": "2026-02-25T20:41:00Z", "command_output": "..."}Examples
示例
Watch and rebuild:
bash
{baseDir}/watch.sh ./src --command "npm run build" --debounce 1Watch for new log files:
bash
{baseDir}/watch.sh ./logs --command "./new-log.sh" --events createWatch config and restart:
bash
{baseDir}/watch.sh --file config.json --command "pm2 restart app"Watch multiple files:
bash
{baseDir}/watch.sh --files "config.json,.env,package.json" --command "npm run build"Watch in background:
bash
{baseDir}/watch.sh ./src --command "make" --daemon --pidfile /tmp/watcher.pidWatch for deleted files:
bash
{baseDir}/watch.sh ./uploads --command "echo 'File deleted'" --events delete监控并重新构建:
bash
{baseDir}/watch.sh ./src --command "npm run build" --debounce 1监控新日志文件:
bash
{baseDir}/watch.sh ./logs --command "./new-log.sh" --events create监控配置文件并重启服务:
bash
{baseDir}/watch.sh --file config.json --command "pm2 restart app"监控多个文件:
bash
{baseDir}/watch.sh --files "config.json,.env,package.json" --command "npm run build"后台监控:
bash
{baseDir}/watch.sh ./src --command "make" --daemon --pidfile /tmp/watcher.pid监控文件删除:
bash
{baseDir}/watch.sh ./uploads --command "echo 'File deleted'" --events deleteDebouncing
防抖机制
The option prevents rapid-fire commands:
--debounce- : Wait N seconds after the last event before running the command
--debounce N - : Track debounce per file independently
--debounce-file
bash
undefined--debounce- : 最后一次事件发生后等待N秒再执行命令
--debounce N - : 为每个文件单独跟踪防抖计时
--debounce-file
bash
undefinedWait 2 seconds after last change
最后一次变更后等待2秒再执行
{baseDir}/watch.sh ./src --command "build.sh" --debounce 2
{baseDir}/watch.sh ./src --command "build.sh" --debounce 2
Debounce per file (each file gets its own timer)
按文件单独防抖(每个文件拥有独立计时器)
{baseDir}/watch.sh ./src --command "process.sh" --debounce-file --debounce 1
undefined{baseDir}/watch.sh ./src --command "process.sh" --debounce-file --debounce 1
undefinedNotes
注意事项
- Uses inotifywait (Linux) or fswatch (macOS) when available
- Falls back to polling on systems without native support
- Command receives and
$FILEenvironment variables$EVENT - Recursive by default for directories
- Can watch thousands of files efficiently
- Clean shutdown with SIGTERM or SIGINT
- 系统支持时会使用inotifywait(Linux)或fswatch(macOS)
- 无原生支持的系统会回退到轮询模式
- 命令会接收和
$FILE环境变量$EVENT - 默认递归监控目录
- 可高效监控数千个文件
- 可通过SIGTERM或SIGINT信号优雅关闭
Dependencies
依赖
- Linux: (inotify-tools package)
inotifywait - macOS: (brew install fswatch)
fswatch - Both: Polling fallback with 1-second interval
- Linux: (来自inotify-tools包)
inotifywait - macOS: (通过brew install fswatch安装)
fswatch - 通用:轮询回退模式,间隔1秒