3

Doing an inotifywait to watch a directory, and trying to exclude all subdirectories from being watched while not excluding the files.

inotifywait -r -q --exclude <pattern> dir/

What to put in <pattern>? Manual for inotifywait specifies:

--exclude <pattern>
Do not process any events whose filename matches the specified POSIX extended regular expression, case sensitive.

There's no -type flag like in find. I've tried (^/) but that seems to exclude everything.

Help appreciated.

5

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.

  • Thanks! But the last version of find will not catch subdirectories within subdirectories, I just tried it to confirm. I need it to catch all directories and ignore all files. In other words, I just want inotifywait to watch changes to files and nothing else. Seemed simple. Oh well. – Sqerstet Nov 3 '14 at 21:53
  • @Sqerstet since I hadn't included the recursive option, that seemed fine. If not, you can simply remove the -maxdepth option to let find find all nested subdirectories. – muru Nov 3 '14 at 21:59
  • Very helpful, much appreciated. – Sqerstet Nov 3 '14 at 23:54
  • Didn't work until I added \n after @%P – brian Nov 8 '18 at 17:47
4

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

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

3

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

  • Apologies, my question was not clear. I want to exclude the directories but not the files. Question updated. – Sqerstet Nov 3 '14 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.