0

If I perform the following:

mkdir /tmp/test
sudo mkdir /tmp/test/test2
sudo chmod 700 /tmp/test/test2
cd /tmp/test  

I would expect find ./ -not -path "*" to determine that test2 matches the not path rule, which should exclude it from being walked at all. However, I get find: ‘./test2’: Permission denied implying that find is attempting to walk the directory irrespective of the rule.

If I run the command with sudo, the directory is not listed, meaning the exclude rule is working, but is applied to output of an initial search, rather than being an explore conditional during the initial root.

I'm trying to achieve a root filesystem walk while excluding certain problematic directories, such as network filesystem mounts which cost considerable time, so I would like to direct find not to explore sub-directories as to reduce the total execution time.

How can I achieve this?

1
  • "I would expect find ./ -not -path "*" to determine that test2 matches the not path rule, which should exclude it from being walked at all." – Things do not work this way, but IF you expected this then you should have expected excluding ./ from being walked at all in the first place. My point is -path "*" is true even for ./ which is tested first. I think -path "*" evaluates as true for any path. Commented Feb 28, 2023 at 15:35

1 Answer 1

2

You need to “prune” the problematic directories; this instructs find to not try to process its contents:

find . -path "*" -prune

More realistically,

$ find . -name test2 -prune
./test2

This still processes test2 itself, but doesn’t attempt to descend into it, so no error is encountered.

0

You must log in to answer this question.

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