Exclude directories from inotifywait

Solution 1:

Since the --exclude option only acts on filenames, there's no direct way to do it. You could try round-about ways like using find to print the name of all directories:

inotifywait --exclude "echo -n (;$(find . -maxdepth 1 -mindepth 1 -type d -printf '%P|');echo )" .

Note that I didn't specify -r, since that will cause newly created subdirectories to be watched too.

This might break with some special characters.


You could also try:

find . -maxdepth 1 -mindepth 1 -type d -printf '@%P\n' > list_of_directories
inotifywait --fromfile list_of_directories .

inotifywait will exclude any files or folders in list_of_directories which begin with @ (all of them do).


If you're using inotifywait with the recursive option, let find list all nested subdirectories as well by removing the -maxdepth restriction (also applies to the first command):

find . -mindepth 1 -type d -printf '@%P\n' > list_of_directories
inotifywait --fromfile list_of_directories . -r

The -mindepth is retained to prevent find from matching ., and thus excluding the current directory as well.

Solution 2:

Just use --exclude '/\..+'. This ignores all files that start with a dot.

(The .+ part is so that it does not exclude your base folder).

Solution 3:

inotifywait only checks subdirectories because of the parameter -r.
Call it without that parameter and it won't watch subdirectories.