Operating System - Linux
1825719 Members
3191 Online
109686 Solutions
New Discussion

Help with script to chown of home directory

 
SOLVED
Go to solution
Matthew Couper
Frequent Advisor

Help with script to chown of home directory

I am having a hard time with effectively listing all the home directories on the system and changing the owner to the associated user. Prior to starting the administrator gave the ownership to root and made all home directories with 777 permission.

I would like to change this, any ideas on a script that would work. I attempted to just do an ls -d -l -t | awk '{print $9}'; cut -c 7-15

However, I can't figure out the best way to get this setup and work with the chown $1 $1.

I am not to sure how to approach this, any ideas?
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Help with script to chown of home directory

Hi Matthew:

This should do what you want:

#!/usr/bin/sh
for D in `ls -ld /home/*|awk '{print $NF}'`
do
chown -R ${D} ${D##*/}:users
done
exit 0

Regards!

...JRF...
Doug O'Leary
Honored Contributor
Solution

Re: Help with script to chown of home directory

Hey;

awk -F: '$3 > 100 {printf("%s %d %s\n", $1, $4, $6)}' /etc/passwd | while read user gid home
do
chown ${user}:${gid} ${home}
done

You could also do a chown -R; however, that'd be a little riskier. I'd suggest echoing the command in the loop above out (echo chown...) to see which home directories and users are getting changed before looking at the -R option.

The $3 > 100 *should* prevent you from changing the ownership of any system related files/directories.

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Matthew Couper
Frequent Advisor

Re: Help with script to chown of home directory

#!/usr/bin/sh
for D in `ls -ld /home/*|awk '{print $NF}'`
do
chown -R ${D} ${D##*/}:users
done
exit 0

----- very close thanks

#!/usr/bin/sh
for D in `ls -ld /home/*|awk '{print $NF}'`
do
chown -R ${D##*/}:users ${D}
done
exit 0

--- this worked
Matthew Couper
Frequent Advisor

Re: Help with script to chown of home directory

BTW I did remove the Recursive chown portion.
Matthew Couper
Frequent Advisor

Re: Help with script to chown of home directory

I did remove the recursive portion, thanks Doug