Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I accidentally the whole thing

drwxr-xr-x  4 imaan                rayhawkpasscom 4096 May  9 14:59 imaan
drwxr-xr-x  4 kimessite            rayhawkpasscom 4096 Mar 15  2011 kimessite
drwxr-xr-x  4 kineticimaging       rayhawkpasscom 4096 Apr  4 15:56 kineticimaging
drwxr-xr-x  4 lynbrookmed          rayhawkpasscom 4096 Jul 18 11:51 lynbrookmed
drwxr-xr-x  5 marcop               rayhawkpasscom 4096 Sep  6 15:39 marcop
drwxr-xr-x  5 mesachiro            rayhawkpasscom 4096 Sep  9 13:21 mesachiro
drwxr-xr-x  4 paramount            rayhawkpasscom 4096 Jun  7 10:39 paramount
drwxr-xr-x  4 paramountco          rayhawkpasscom 4096 Jun 16 13:50 paramountco
drwx------  2 rayhawk              rayhawkpasscom 4096 Jul 13 15:30 rayhawk

I mistakenly changed the groups of all these directories to an incorrect group.

The group name should be the same as the owner (3rd column) or the folder (last column)

Is there a way I can automatically chgrp recursively based on owner/dir name?

share|improve this question

migrated from stackoverflow.com Oct 10 '11 at 10:58

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers

up vote 2 down vote accepted

From the directory in question, I'd run: for i in *; do chown -R $i:$i $i; done

(The "-R" in chown, only if you initially did a recursive chgrp)

share|improve this answer
add comment

If the directory name is different from the owner and you want to change group ownership based on the owner, try this:

# ls -d */ | while read d; do chgrp -R $(stat -c %U "$d") "$d"; done
share|improve this answer
add comment

This seems correct to me, but your YMMV and might sell your firstborn for organ donation, so be careful:

find . -type d -maxdepth 1 -exec chgrp -R {} {};
share|improve this answer
    
# find . -type d -maxdepth 1 . -exec chgrp -R {} {}; find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] –  Steve Robbins Oct 5 '11 at 22:02
    
second . isnt needed –  Sirex Oct 10 '11 at 11:20
add comment

Use the either the group or gid option in find and make the change to the result from the find. Here is a example with listing directories owned by mySQL

[root@kvm0001 ~]# find /var/ -type d -group mysql -exec ls -la {} \; | more
total 12
drwxr-xr-x.  2 mysql mysql 4096 Oct  9 17:09 .
drwxr-xr-x. 25 root  root  4096 Oct 10 03:52 ..
-rw-rw----.  1 mysql mysql    5 Oct  9 17:09 mysqld.pid
total 20500
drwxr-xr-x.  5 mysql mysql     4096 Oct  9 17:09 .
drwxr-xr-x. 33 root  root      4096 Oct  4 21:01 ..
drwx------.  2 mysql mysql     4096 Oct  4 21:23 cacti
-rw-rw----.  1 mysql mysql 10485760 Oct  9 17:06 ibdata1
-rw-rw----.  1 mysql mysql  5242880 Oct  9 17:09 ib_logfile0
-rw-rw----.  1 mysql mysql  5242880 Oct  4 20:19 ib_logfile1
drwx------.  2 mysql mysql     4096 Oct  4 20:19 mysql
srwxrwxrwx.  1 mysql mysql        0 Oct  9 17:09 mysql.sock
drwx------.  2 mysql mysql     4096 Oct  4 20:19 test

In this case, you want to change the directories found to a different group user. In that case, you would do this:

find /path/to/directory -type d -group rayhawkpasscom -exec chgrp -Rv {}\;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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