Dan
Dan

Reputation: 951

Using find in OS X to identify then correct permissions

I'm trying to use find (or a better method, as this will then go into a shell script) to do the following:

This only needs to look one level deep, I.e. one level with a user's directory in /Users

This is what I've come up with so far but it doesn't quite do the trick:

find /Users ! -perm 0700 -type d -depth 2 | grep -v Public | grep -v Sites | /usr/bin/xargs -0 chmod 700

thanks for your suggestions!

Dan

Upvotes: 2

Views: 1469

Answers (1)

gniourf_gniourf
gniourf_gniourf

Reputation: 46823

This should do:

find /Users -maxdepth 2 \! -perm 0700 \! -name Public \! -name Sites -type d -exec chmod 700 {} +

No pipes, just one command :)

Note: I've changed your -depth 2 to -maxdepth 2 and moved it in front... just because GNU find's -depth doesn't work like this and I believe this is how it would be written with GNU find. Now, I don't know about the find you have installed on your system, so YYMV, like they say...

I would also use chmod's -v option, if available, just to know what's happening... something like:

`chmod -v 700 {}`

Experiment with the command I gave you like so:

find /Users -maxdepth 2 \! -perm 0700 \! -name Public \! -name Sites -type d -exec echo chmod 700 {} +

with the echo there, so that it's harmless. It will only print on your screen the commands that will be executed when the echo removed. Remove the echo only when you're happy with the way it behaves.

Edit. It seems that OS X and GNU versions of find differ in the use of -depth/-maxdepth. So here's a similar command for OS X (see Dan's comment below):

find /Users -depth 2 \! -perm 0700 \! -name Public \! -name Sites -type d -exec echo chmod 700 {} +

Cheers!

Upvotes: 2

Related Questions