recursive chmod

Ben Pfaff pfaffben@msu.edu
27 Nov 2000 17:50:19 -0500


Edward Glowacki <glowack2@msu.edu> writes:

> On 27 Nov 2000, Ben Pfaff wrote:
> > Edward Glowacki <glowack2@msu.edu> writes:
> > 
> > > find / -type d -exec chmod 0755 {};
> > > find / -type f -exec chmod 0644 {};

My tests here show that, at least with GNU find, the `;' must be
a separate argument.  Under bash:

	find / -type d -exec chmod 0755 {} \;

(The naked {} make me nervous for some reason, so I might escape
those, too.)

> > Or, if you have GNU tools, the following is safer:
> > 
> > 	find / -type d -print0 | xargs -0 chmod --
                                                ^ oops: insert 0755 here
> 
> I'm not doubting you on this, but could you explain why it's safer?
> Just curious...

Well, there's only really one change that makes it safer: the --,
which indicates that everything following is not an option.  GNU
tools, in general, accept options following nonoption arguments,
so a file whose name begins with a `-' could otherwise cause
problems.  (chmod may be an exception, I'm not sure.)

Also, I wasn't sure whether `find' executed -exec using the shell
or not.  A quick strace demonstrates that it doesn't, though, so
that's one less worry.  (If it did, though, you'd have to worry
about shell metacharacters in filenames, too.)

The find-xargs variant may be faster, too: instead of running
`chmod' once for every file or directory, it only runs it once
for every N files or directories, where N is a large,
system-dependent number.