Monitor directory listing for changes?

前端 未结 2 1356
说谎
说谎 2021-01-12 20:40

On a unix system, how do I monitor (like how \'tail\' works) a directory for changes made to files - either new ones created, or size changes, etc.

Looking for a com

2条回答
  •  太阳男子
    2021-01-12 21:37

    Most unix variants have an API for this, but it's not standardized. On Linux, there is inotify. On the command line, you can use inotifywait. Usage example:

    inotifywait -m /path/to/dir | while read -r dir event name; do
      case $event in
        OPEN) echo "The file $name was created or opened (not necessarily for writing)";;
        WRITE) echo "The file $name was written to";;
        DELETE) echo "The file $name was deleted ";;
      esac
    done
    

    Inotify event types are often not exactly what you're trying to notice (e.g. OPEN is very wide), so don't feel bad if you end up making your own file checks.

提交回复
热议问题