Search code examples
bashterminalfindchgrp

Set group of directory to name of directory


I have a bunch of folders I would like to find and change the group to the directory name.

/SchoolA/
/SchoolB/
......
/SchoolZZZ/

I can't figure it out - but would like something like:

find . -type d -exec chgrp {} {} \;

I get illegal group name because that includes ./ on the group. Anyway to eliminate the ./

Needs to be something like

find . -type d -exec chgrp NODOTSLASH({}) {} \;

*EDIT*

I am closer with the help of the posts below - but it still prints the "." directory. How do I get rid of that?

find . \( ! -path '^.*' \)  -type d -maxdepth 1 | sed -e 's/\.\///g' | awk '{print "\x22"$0"\x22","\x22"$0"\x22"}'

Solution

  • find . -type d | sed -e 's/\.\///g' | awk '{print $1, $1}' | xargs chgrp
    

    A more adhoc approach to avoid dots .

    find . -type d | sed -e 's/\.\///g' -e 's/\./avoid/g' | grep -v avoid | awk '{print $1"\t"$1}' | xargs chgrp
    

    The above also applies tab space between the two words.