1

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"}'
broccolifarmer
  • 465
  • 7
  • 15

2 Answers2

1
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.

iamauser
  • 11,119
  • 5
  • 34
  • 52
  • This is very close - however, spaces (which I didn't specify would be in there) do not work correctly. However - I did modify some to try to get the correct result - but still not quite right. Thanks for you help so far! See original post for how I modified. Thanks again! – broccolifarmer Aug 17 '13 at 16:07
  • Added more info to my answer... This should solve the `dot` problem. I have tested it on my Mac, and it seems to be working... – iamauser Aug 17 '13 at 16:44
  • Great - that fixed the . issue. I also used $0 instead of $1 and surrounded it with quotes as the group/folder names I have did have spaces. Thanks for your help! – broccolifarmer Aug 17 '13 at 16:56
0

Use a while loop with formatting:

find . -type d -printf '%f\t%p\n' | while IFS=$'\t' read A B; do chgrp "$A" "$B"; done
konsolebox
  • 72,135
  • 12
  • 99
  • 105