How to monitor a complete directory tree for changes in Linux?

后端 未结 8 2046

How can I monitor a whole directory tree for changes in Linux (ext3 file system)?

Currently the directory contains about half a million files

相关标签:
8条回答
  • 2020-12-02 10:04

    inotify is the best option when you have many subdirectories but if not I am used to using this command below:

    watch -d find <<path>>

    0 讨论(0)
  • 2020-12-02 10:06

    Especially for large or complex monitoring tasks in which you want to trigger events based on what you see, check out Watchman A file watching service. Here is a simple example to run a tool named minify-css whenever a CSS file is changed:

    $ watchman watch ~/src
    $ watchman -- trigger ~/src buildme '*.css' -- minify-css
    

    It does comprehensive logging, can efficiently handle multiple watches that overlap in a directory structure, can be administered from the command line or via json, and much more. See also

    • Watchman – A File and Directory Watching Tool for Changes

    It is available via Debian Sid and Ubuntu 20.04, and has nearly made it in to Fedora twice from what I can see (1450590 and 1564720).

    0 讨论(0)
  • 2020-12-02 10:17

    Wasn't fanotify supposed to provide that capability eventually? Quoting LWN:

    fanotify has two basic 'modes' directed and global. [...] fanotify global instead indicates that it wants everything on the system and then individually marks inodes that it doesn't care about.

    I lost track what its latest status was, though.

    0 讨论(0)
  • 2020-12-02 10:20

    I have a different suggestion, only for changes in the files, and record history changes

    use git

    cd /folder_to_monitor
    git init
    git add *
    git commit -m "first snapshot"
    

    so after you make the changes

    git diff
    
    0 讨论(0)
  • 2020-12-02 10:21

    To my knowledge, there's no other way than recursively setting an inotify watch on each directory.

    That said, you won't run out of file descriptors because inotify does not have to reserve an fd to watch a file or a directory (its predecessor, dnotify, did suffer from this limitation). inotify uses "watch descriptors" instead.

    According to the documentation for inotifywatch, the default limit is 8192 watch descriptors, and you can increase it by writing the new value to /proc/sys/fs/inotify/max_user_watches.

    0 讨论(0)
  • 2020-12-02 10:24
    $ inotifywait -m -r /path/to/your/directory
    

    This command is enough to watch the directory recursively for all events such as access, open, create, delete ...

    0 讨论(0)
提交回复
热议问题