2

This question already has an answer here:

I'm trying to run the following:

find . -user user_name -o -group user_group -exec chown root. {} \;

But after running the command it doesn't alter the ownership of any files. If I run without the -exec it produces a list of all the files I expect to be modified correctly.

If I use:

find . -user user_name -exec chown root. {} \; find . -group user_group -exec chown root. {} \;

The files are updated as expected. I see in the man page that:

expr1 -o expr2
          Or; expr2 is not evaluated if expr1 is true.


expr1 -or expr2
          Same as expr1 -o expr2, but not POSIX compliant.

However, the files are owned by user_name:user_group. The first case should evaluate to true and run the exec command unless I'm misunderstanding something here. Can someone explain why this is working this way?

Some system info if needed:

find (GNU findutils) 4.4.2 CentOS release 6.8 (Final)

marked as duplicate by Stéphane Chazelas, Jeff Schaller, GAD3R, Satō Katsura, don_crissti Oct 3 '16 at 13:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I suspect they need to be grouped using \( \).. can you try: find . \( -user user_name -o -group user_group \) -exec chown root. {} \; – Sundeep Oct 3 '16 at 6:12
2

Logical operators in find syntax have lower precedence and in your example -o divides -user user_name and -group user_group -exec chown root. {} \;.

Check the following:

find . -user user_name -exec chown root. {} \; -o -group user_group -exec chown root. {} \;

To avoid duplication use parentheses to evaluate the two test expressions together:

find . \( -user user_name -o -group user_group \) -exec chown root. {} \;

Not the answer you're looking for? Browse other questions tagged or ask your own question.