Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Okay so I have about 100 directories in my home folder each owned by the home user itself. Sometimes I have to manually replace some files as root so the permissions are disturbed and some files are no longer owned by that user. To fix this, I always have to use the following command within the home directory:

chown -R user:user user/

Now I'm looking at some shortcut because I can't do this for all the 100 users out there.

I'm wondering what would be done by:

chown -R *:* *
share|improve this question

migrated from serverfault.com Jul 18 '13 at 18:39

This question came from our site for system and network administrators.

cd home; for i in * do chown -R ${i}:${i} ${i}; done – Jenny D Jul 18 '13 at 10:49
Awesome. Thanks. Gonna test this now. – Asad Moeen Jul 18 '13 at 10:58
@JennyD You missed a semicolon after the * – val0x00ff Jul 18 '13 at 11:00
@val0x00ff Thanks for catching that! – Jenny D Jul 18 '13 at 13:06
up vote 2 down vote accepted

The full answer would be

1) Open a file like vi chown_homedirs

2) Put in the next code


cd /home &&    
for h in *
    chown -R "$h:$h" "$h"; 

3) Save the file

4) chmod +x chown_homedirs

5) run ./chown_homedirs

NOTE: If user directories have spaces in them you always need to quote the expansion. If you don't quote the expansion things might fail and pose you to unexpected results.

share|improve this answer
Thanks. Works great. – Asad Moeen Jul 18 '13 at 15:04
I wonder why this is needed and what it does shopt -s extglob – Asad Moeen Jul 18 '13 at 16:11
@Asad Moeen mywiki.wooledge.org/glob – val0x00ff Jul 19 '13 at 7:14

You can use stat to get the user and group ownership of a file then use these to update the files, something like

cd /home
for file in *
    user=$(stat -c %U "$file")
    group=$(stat -c %G "$file")
    echo chown -R "$user":"$group" "$file"

This deals with the situation where the owner/group/directory aren't all the same. Obviously remove the echo if it out tests ok for you.

share|improve this answer
Use caution when writing scripts that cd to a target and perform operations. Consider this script is run by an administrator on a server where /home is in a bad state and the administrator is currently in /var. Everything in var would be chown -R root:root. I would recommend iterating directly on /home/* or using something like if [ $? -ne 0 ]; then echo Failed to cd to /home. Exiting!; exit 1; fi. – Kyle Smith Jul 18 '13 at 11:16
@KyleSmith: There are lots of things wrong with the script from the POV of being complete, it's really just demonstrating the use of stat rather than blindly assuming the ownership of the files. – Iain Jul 18 '13 at 11:23
@KyleSmith Bash uses [[]] keyword for testing. [ test command is used only if you are writing for !sh. Also you should not be needing to check the status code, instead ! cd /home && echo "failed" >&2; exit 1 – val0x00ff Jul 18 '13 at 11:25
@Iain: Understood, just want to make sure OP or anyone else considering a script is thinking of the caveats. – Kyle Smith Jul 18 '13 at 11:26
@val0x00ff: Using && is checking the status code. My code is just a more verbose way of accomplishing the same. – Kyle Smith Jul 18 '13 at 11:29

Your Answer


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