Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 professional system and network administrators.

4  
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
3  
@JennyD You missed a semicolon after the * –  val0x00ff Jul 18 '13 at 11:00
1  
@val0x00ff Thanks for catching that! –  Jenny D Jul 18 '13 at 13:06

2 Answers 2

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

#!/bin/bash

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

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

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

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

 
discard

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