Tuesday, April 30, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 10771  / 2 Years ago, fri, september 16, 2022, 6:33:04

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.


More From » regex

 Answers
7

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
' > 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
' > 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.


[#22561] Saturday, September 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
berlorful

Total Points: 346
Total Questions: 90
Total Answers: 99

Location: Monaco
Member since Tue, Nov 30, 2021
2 Years ago
berlorful questions
Thu, Sep 2, 21, 10:12, 3 Years ago
Sun, May 9, 21, 20:55, 3 Years ago
Mon, Jan 16, 23, 23:19, 1 Year ago
Mon, Aug 29, 22, 05:43, 2 Years ago
;