2

I'd like to ignore a directory that has not been created at the time I start inotifywait. I have an empty directory test:

ubuntu@foo:~$ ls -lah test/
total 8.0K
drwxrwxr-x  2 ubuntu ubuntu 4.0K Sep  5 20:00 .
drwxr-x--- 13 ubuntu ubuntu 4.0K Sep  5 19:56 ..

I start inotifywait like this:

ubuntu@foo:~$ inotifywait -mr -e CREATE -e MODIFY -e DELETE -e MOVE /home/ubuntu/test/ @/home/ubuntu/test/log
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

Then, in another terminal, I create a directory log and put some content there:

ubuntu@foo:~/test$ mkdir log
ubuntu@foo:~/test$ cd log/
ubuntu@foo:~/test/log$ echo "foo" > foo

This is the inotifywait output:

/home/ubuntu/test/ CREATE,ISDIR log
/home/ubuntu/test/log/ CREATE foo
/home/ubuntu/test/log/ MODIFY foo

However, if I close the script and start it again and put some more content into the file, no events are triggered.

Is it possible, to ignore a "soon to be created"-directory in inotifywait?

1 Answer 1

2

This is easily done by replacing the @ exclusion prefix by the --exclude option with a simple pattern (regular expression). For example,

--exclude /home/ubuntu/test/log'$'

By adding a $ (match end-of-string) we ensure that creating a file such as test/log2 is not also wrongly matched and excluded. Also add a ^ at the start to ensure we don't match the path at some greater depth:

--exclude '^'/home/ubuntu/test/log'$'

The exclusion of the directory is sufficient to exclude new files under it, unless the directory already exists, bizarrely. To cope with that case, replace the $ by an alternate $ or /, i.e. ($|/):

--exclude '^'/home/ubuntu/test/log'($|/)'

However, testing seems to show that (for my version 3.14?) only the last --exclude option is used, which is not documented, and rather unexpected. So if you have several directories to exclude, you need to combine them into a single regexp. This does not need to be complicated if you are not familiar with the syntax, simply put the list inside ^(...|...|...)($|/), for example:

--exclude '^(/home/ubuntu/test/log|/home/abc/log|/usr/def)($|/)'

If your paths contain special regexp characters like .[]^$()*+?|{}, you need to look up how to escape them.

1
  • Thank you so much, this works like a charm!
    – 23tux
    Sep 7, 2022 at 4:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .