Executing a bash script upon file creation

后端 未结 3 839
既然无缘
既然无缘 2020-12-08 17:57

I am looking write a small bash script to, when launched, watch a directory for any newly created files. If a new file appears, I want its presence to trigger a second scrip

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 18:16

    You can do this with the magical inotify tool :

    inotifywait -r -m ./YOUR_MONITORED_DIR |
        while read a b file; do
            [[ $b == *CREATE* ]] && ./another_script "$file"
        done
    

    This method have the big advantage to avoid polling every N seconds.

    Inotify (inode notify) is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify, which had similar goals.

    http://en.wikipedia.org/wiki/Inotify
    See inotify doc

提交回复
热议问题