Question about find - excluding directories

Matthew Seaman matthew at
Mon Oct 15 06:14:51 UTC 2012

On 15/10/2012 01:32, Paul Schmehl wrote:
> I want to use find to locate files that don't belong to a certain user
> but should belong to that user.  But there are subdirectories I want to
> exclude.
> I have tried using this, but it doesn't work:
> find /path/to/dir -type d ! -uid num \( -type d ! -name dirname -prune \)
> If I leave off the part in parentheses, it finds all the files I'm
> looking for but also files in the subdirs I'm not interested in.
> If I add the parentheses, it doesn't find any files at all.
> This is FreeBSD 8.3 RELEASE.
> So how can I find these files without descending into directories I'm
> not interested in?

Completely untested, but the usual thing with find(1) is down to the way
it evaluates its arguments from left to right in a lazy fashion.  So, if
it has enough to know it is going to generate a true or false result
after '-type d ! -uid num', it won't then go on to evaluate the rest of
the line, meaning it will never see the effects of '-prune'.

If you want to have exceptions, it is generally better to put them
earlier in the command line:

find /path/to/dir \( -type d ! -name dirname -prune \) -type d ! -uid num

Or you could use '-path' to match any path containing 'dirname' without
the bracketed subexpression:

find /path/to/dir -type d ! -path 'dirname*' ! -uid



Dr Matthew J Seaman MA, D.Phil.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 264 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the freebsd-questions mailing list