0

I have the need to chmod files which are in a certain group. I have come up with this construct:

find . -group groupname -exec sh -c 'su -c "chmod -R u+w,g-w,o-w,g+r,o+r ." - groupname {}' \;

This works, but fails when the filename has a bracket ( or ) in it.

Any ideas?

  • Use "{}" to put double-quotes around the discovered filename. – DopeGhoti Feb 15 '17 at 21:39
  • your missing the standard find -name option. All you need is find . -name '*' -group groupname if in fact you want to capture all files. – jiveturkey Feb 15 '17 at 21:41
  • @jnbbender why would you want -name '*'? That's a no-op. – roaima Feb 15 '17 at 21:45
  • @roaima It was failing for me without it. Works just fine with it. – jiveturkey Feb 15 '17 at 21:51
  • I don't get why you might need two shs. – phk Feb 15 '17 at 21:56
0

You have to set IFS variable to $(echo -en "\n\b") , for example:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
find . -group groupname -exec sh -c 'su -c "chmod -R u+w,g-w,o-w,g+r,o+r ." - groupname {}' \;
IFS=$SAVEIFS
2

The way you're using the {} interpolation leaves the filename unquoted, so it's a bit like running chmod u=rw some(filename).txt, which will fail in the way you've described.

One solution is to move the {} out of a quoted string so that find can handle it properly itself.

Another problem is that you seem to be splitting the chmod command, with part of it inside the su and part of it outside. (Demonstrate this by replacing sh with echo.) The result is a broken command, and I'm not entirely sure what you're trying to achieve.

Try this (remove the echo when you're sure it's doing what you want):

su -c "
    find . -group groupname -exec echo chmod -R u+w,go+r,go-w {} \;
"

Do you really intend to apply a recursive chmod to every directory that happens to have group groupname? Perhaps you should consider -type d in the set of predicates.

  • Hi, yes I have since come to the conclusion that this is too slow, so I just use chmod --silent – jdog Feb 15 '17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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