How do I (recursively?) Track the contents of new directories using inotify?

First, I want to start by using inotify to monitor a specific directory (main directory) for files and subdirectories. If a new directory is added to this main directory, how could I track this subdirectory using inotify? How can I track the new directory in this subdirectory of the main directory?

I think that adding it to the watch is easy using the inotify_add_watch() function, but I donโ€™t know how to get the correct relative path to files and directories in subdirectories (use for similar synchronization with Dropbox to another location while maintaining the correct directory tree, for example) .

+6
source share
1 answer

Well, the fastest to implement (but not the fastest actually):

  • Create a source directory tree by recursively exploring children; An example in C / Linux can be found here:

    http://www.lemoda.net/c/recursive-directory/

  • Add hours for each subdirectory; When something has been changed or changed, you can analyze all the recursive data and see the differences. Something similar was discussed here: How to control a folder with all subfolders and files inside?

  • If you donโ€™t like this solution, you can try to create a polling mechanism so that you can re-check the entire structure using a stream at a specific time interval.

Hope this helps!

+6
source

Source: https://habr.com/ru/post/957931/


All Articles