5

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Kevin From
  • 142
  • 2
  • 9

1 Answers1

5

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 implied by the conjunction of primaries—, but also both predicates inside should be negated.

find . \( ! -user user -o ! -group group \) -exec chown user:group {} +
oguz ismail
  • 1
  • 16
  • 47
  • 69