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.

My client had me deploy some folders out to a bunch of home directories for his customer websites. I did this with a Bash script, but it ended up using the root account permissions.

How do I make a Bash script that takes each folder under /home/user (not hidden files or folders), gets the user and group ownership of that folder, and then does a chown -R {user}.{group} /home/user?

The servers are running CentOS Linux.

share|improve this question

6 Answers 6

up vote 6 down vote accepted

I have to give Kyle Brandt complete credit above. So, if you like this answer below, click the Up triangle on his post to lift his status, please.

However, I improved upon his routine and felt it my duty to post it here and mark it as the final answer.

All I added to Kyle's routine was ensure that we're only touching the home dir, thus the line with the asterisks in it. Then, I ensure that this home dir actually still exists. After that, I do the chown statement. And just like Kyle said -- remove the "echo" keyword and it will actually conduct the task. Then, I added "-R" on the chown to make it work recursively in case the problem might be deeper into one's home dir.

#!/bin/bash

while IFS=':' read -r login pass uid gid uname homedir comment; do
    if [[ "$homedir" = **/home/** ]]; then
        if [ -d "$homedir" ]; then
            echo chown -R $uid:$gid "$homedir";
        fi
    fi
done < /etc/passwd
share|improve this answer
#!/bin/bash

for f in $( ls /home/ );
  do chown -R $f:yourgroup /home/$f
done

There is no sanity checking in this and I wrote it without any testing, so be careful.

(BTW, the requirement of "no hidden files or folders" will be met by the fact that a hidden file in Unix is just a regular file with a . before it, and .username will not be a valid user for chown).

share|improve this answer

I think the way you are asking is kind of backwards. You don't want to take each folder and then find the user, rather you want to take the user and find their home folder.

#!/bin/bash
while IFS=':' read -r login pass uid gid uname homedir comment; do 
    echo chown $uid:$gid "$homedir"; 
done < /etc/passwd

You will need to remove the echo of course and you will need to run this with root permissions. I also always recommend a while loop instead of a for loop over ls myself. You can save this loop for doing anything with /etc/passwd.

share|improve this answer

This might help

   #!/bin/bash
    for file in `ls -a | grep -v '^\.'`
    do
            if [[ -d $file ]]
            then
                    fowner=`ls -ld $file | nawk '{print $3}'`
                    fgroup=`ls -ld $file | nawk '{print $4}'`
                    chown -R $fowner:$fgroup $file
            fi
    done
share|improve this answer

Use getent to get the user name, group name, and home directory of each user whose user ID is greater than or equal to 500, and apply them via chown -R. Also, nowadays it's user:group.

share|improve this answer

for i in ls -l /home/ | awk '{print $3}';do chown -R $i /home/$i;done
for i in ls -l /home/ | awk '{print $4}';do chgrp -R $i /home/$i;done

share|improve this answer

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.