#728·fsnotify

Watching only a directory for changes

Author: WhyNotHugoCreated Feb 16, 2026Updated Feb 16, 2026

I'm using fsnotify for ImapGoose, which watches a whole tree of Maildir for changes in email.

In Maildir, files are immutable: they can be created, deleted or moved, but never changed. ImapGoose only needs to watch for directory changes, but not each file itself.

Currently, fsnotify recursively monitors all files inside directories too. With kqueue, this consumes up hundreds of thousands of file descriptors, which quickly fails, the limit typically being orders of magnitude lower.

Is it possible to only watch directories for changes (i.e.: don't monitor files inside directories too)? I don't mind if I get superfluous events on some platforms, but I want to avoid opening hundreds of thousands of file descriptors on BSD.