LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   chown all folder and subfolders in home with their own user (https://www.linuxquestions.org/questions/linux-newbie-8/chown-all-folder-and-subfolders-in-home-with-their-own-user-926284/)

AngelDeaD 01-29-2012 03:31 AM

chown all folder and subfolders in home with their own user
 
I have 52 directory in home folder, some files in those folders, are chown-ed to root.
I need script who chown folders and all files and folders in them to user named as folder in home.

If username john then there is folder in home named as john.

fukawi1 01-29-2012 03:45 AM

script? use chown.
Specifically, chown -R

"man chown" for how to use chown.

ophirg 01-29-2012 03:47 AM

use the "-R" flag. it's a very common flag in file related programs.
it applies the command recursively all the sub-directories.

Code:

chown -R john /home/john

AngelDeaD 01-29-2012 03:49 AM

Quote:

Originally Posted by fukawi1 (Post 4587275)
script? use chown.
Specifically, chown -R

"man chown" for how to use chown.

I want script. I want do it very very fast.
52 times type
chown -R username:username username
It's lil bit hard.

Cedrik 01-29-2012 03:56 AM

Code:

for dir in /home/*; do
  username=$(basename $dir)
  echo "changing ownership to $username in $dir"
  chown -R $username:$username $dir
done


fukawi1 01-29-2012 03:58 AM

So you want to do it for every user? This wasn't clear in your original question.

Code:

/home # for i in *; do chown -R $i:$i /home/$i; done
should do it, although it will likely barf at the "lost+found" directory, if there is one..

AngelDeaD 01-29-2012 04:23 AM

Thank's everyone and sorry for my bad english explaining. It's not my native lang.

bakh 07-02-2018 01:40 PM

Thanks All
 
Thanks guys for your replies. This helped resolve something on one of our servers.

MadeInGermany 07-02-2018 04:39 PM

The SysV/Posix chown follows symlinks by default.
Say there is a symbolic link to /etc somewhere in the target path, then /etc gets a new owner!
Better run chown -hR ...
Portable is
Code:

chown -hR $username:$username $dir ||
chown -R $username:$username $dir


keefaz 07-02-2018 06:22 PM

Or maybe use a find command to filter files you want to change


All times are GMT -5. The time now is 04:09 AM.