linuxbashfind

Using find to locate files not owned by USER or GROUP


I need to find files and folders in CentOS 7 where they are either not owned by user or not owned by group.

I have 3 test directories:

root:root test1
root:group test2
user:root test3

I need to be able to find all 3 directories, so I can chown user:group, in one find command.

I've tried these:

find . ! -user user -or ! -group group
find . ! \( -user user -or -group group \)

But none of them work. Have I misunderstood something?

I've done some further testing, and by using the first example: find . ! -user user -or ! -group group -print0 I can only get results from test1 directory


Solution

  • not (X or Y) and (not X or not Y) are different things, negation is not a distributive operation. You indeed need parentheses there as -o has a lower precedence than -a that is implied by the conjunction of primaries, but both predicates inside should also be negated.

    find . \( ! -user user -o ! -group group \) -exec chown user:group {} +