Monitor directory listing for changes?

那年仲夏 提交于 2019-12-01 05:19:16

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.

you can craft your own then if you don't want to install tools. Just an idea. Create a base line file of your directory using find command. Use a loop or cron job, find the directory using the same parameters, and check the new file against the base line file. Use a tool like diff to get the differences..

eg

find /path [other options] >> baseline.txt 
while true #or use a cron job
do
  find /path [same options] >> listing.txt
  diff baseline.txt listing.txt
  # do processing here...
  mv listing.txt baseline.txt  # update the baseline.
  sleep 60
done
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!