shellunixrecursionfindchgrp

Unix shell: Recursive find to chgrp on all files and dirs EXCEPT a single named dir


I see a few requests that are similar to mine using the prune action, but what I want to do is recursively navigate through a docroot:

/opt/web

which contains several files and directories:

/opt/web/foo.html     (file)
/opt/web/bar.txt      (file)
/opt/web/DONOTCHGRP   (dir)
/opt/web/assets       (dir)

I want to go through the whole docroot and if any files are not group owned by "mygroup" then change the group to "mygroup" and set the group write permission bit, except completely ignore the DONOTCHGRP directory itself and its contents.

I currently have the command to do the chgrp/chmod with no filtering on anything:

find /opt/web -not -group mygroup |
    xargs -I {} sh -c '{ chmod g+w {}; chgrp mygroup {};}'

I just can't figure out how to completely skip the DONOTCHGRP directory. Any help will be appreciated.


Solution

  • find does that quite well for you with the -path and -prune options. For example to find all directories except one named /opt/web/DONOTCHGRP under the /opt/web directory:

    find /opt/web -path /opt/web/DONOTCHGRP -prune -exec <script> '{}' \;
    

    Then simply include your chmod g+w "$1"; chgrp mygroup "$1"; in a short script and make it executable (the < and > above are just for emphasis and not part of the actual command). find will call the script for all files and directories, except /opt/web/DONOTCHGRP and the files/dirs below it.