inotifywait
跳转到导航
跳转到搜索
监视对某个文件/目录的改变。
示例
仅监视
监视对 home 目录及其子目录的操作:
inotifywait -mr $HOME
执行命令:
touch abcd.txt && echo 111 > abcd.txt && rm abcd.txt
输出:
/home/lily/test/ CREATE abcd.txt /home/lily/test/ OPEN abcd.txt /home/lily/test/ ATTRIB abcd.txt /home/lily/test/ CLOSE_WRITE,CLOSE abcd.txt /home/lily/test/ MODIFY abcd.txt /home/lily/test/ OPEN abcd.txt /home/lily/test/ MODIFY abcd.txt /home/lily/test/ CLOSE_WRITE,CLOSE abcd.txt /home/lily/test/ DELETE abcd.txt
带时间戳的输出格式
inotifywait -m -r . --format='%T %e %w%f' --timefmt='%Y-%m-%d %H:%M:%S'
在特定文件改变时执行命令
inotifywait -q -e create -m qcore --format '%f' | while read f; do [[ $f == static && $(readlink qcore/static) == */assets ]] && git checkout qcore/static; done
在文件改变时执行命令
在目录中的特定类型文件改变之后,延迟终止另一监听进程(该进程可通过其它方式自动重启):
inotifywait -r -q -e create,close_write,moved_to -m project --format '%f' | grep --line-buffered '\.py$' | run_on_silence -- fuser /tmp/some.sock -sk -TERM