Josh
Josh

Reputation: 49

Bash Find Files/Folders that Don't Belong to Permissions - Execute Multiple Commands

I have the command close, but it doesn't seem to be executing the commands.

find /folder \! -user USERNAME -o -not -group GROUPNAME -o -not -perm 750 -exec chown USERNAME:GROUPNAME {} \; -exec chmod 750 {} \;

Upvotes: 1

Views: 60

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

find's manual says (highlight's mine):

expression -o expression

Alternation of primaries; the OR operator. The second expression shall not be evaluated if the first expression is true.

For your invocation that means, if the file currently being processed doesn't belong to user USERNAME or group GROUPNAME, find will leave it as it is and skip to next one, and if its permission bits doesn't match 750, then chown and chmod will be run.

To make it work, you need to place parens around expressions to force their precedence, like:

find /folder ! \( -user USERNAME -group GROUPNAME -perm 750 \) \
     -exec echo chown USERNAME:GROUPNAME {} \; \
     -exec echo chmod 750 {} \;

If its output looks good, remove echos.

And also note that both chown and chmod can operate on multiple files at once, so you can replace \;s with +s and save time.

Upvotes: 2

Related Questions