Monitor directory listing for changes?

前端 未结 2 1355
说谎
说谎 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.

    0 讨论(0)
  • 2021-01-12 21:40

    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
    
    0 讨论(0)
提交回复
热议问题