Shell – inotifywait exclude file types

inotifyshell-script

I'm using inotifywait in a script and wondering if there is a way to exclude hidden files from being watched?

I can't seem to determine the regex pattern to exclude hidden files.

Best Answer

I assume you mean filenames that start with a dot (.), you can ignore those. The thing with inotifywait --exclude is that the pattern appears to be matched against the full path of the file, so you'll need to take that into account.

So, if you give inotifywait the directories foo and bar to watch, then the patterns match against filenames like foo/something, bar/somethingelse. As usual in regexes, you need to escape the dot.

This should watch for all creates in the current directory except for dotfiles (it's a regex, so we need to escape the dots):

inotifywait -ecreate -m --exclude '^\./\.' .

Or, less specifically, exclude dotfiles in any directories, by looking for the combination of a slash and dot:

inotifywait -ecreate -m --exclude '/\.' foo bar

That, of course, will not work if you're watching a directory with a leading dot in some part of the path; it'll match everything in that path.

Related Question