LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2012, 03:31 AM   #1
AngelDeaD
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Rep: Reputation: Disabled
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.

Last edited by AngelDeaD; 01-29-2012 at 03:33 AM.
 
Old 01-29-2012, 03:45 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 191Reputation: 191
script? use chown.
Specifically, chown -R

"man chown" for how to use chown.
 
Old 01-29-2012, 03:47 AM   #3
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 13.10
Posts: 134

Rep: Reputation: 34
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
 
Old 01-29-2012, 03:49 AM   #4
AngelDeaD
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by fukawi1 View Post
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.
 
Old 01-29-2012, 03:56 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Code:
for dir in /home/*; do
  username=$(basename $dir)
  echo "changing ownership to $username in $dir"
  chown -R $username:$username $dir
done
 
1 members found this post helpful.
Old 01-29-2012, 03:58 AM   #6
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 191Reputation: 191
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..
 
1 members found this post helpful.
Old 01-29-2012, 04:23 AM   #7
AngelDeaD
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank's everyone and sorry for my bad english explaining. It's not my native lang.
 
Old 07-02-2018, 01:40 PM   #8
bakh
LQ Newbie
 
Registered: Jul 2018
Location: Uganda
Posts: 1

Rep: Reputation: Disabled
Thumbs up Thanks All

Thanks guys for your replies. This helped resolve something on one of our servers.
 
Old 07-02-2018, 04:39 PM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 902

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
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
 
Old 07-02-2018, 06:22 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Or maybe use a find command to filter files you want to change
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change user's home folder mruda Linux - Newbie 1 11-23-2011 04:35 PM
Scripts to list folder contents and copy images from folder and subfolders brunces Linux - Newbie 6 11-03-2011 01:23 PM
Customized home folder for the every new user deepak_message Linux - General 2 07-08-2011 12:35 PM
How to chown of folder...ie-/home directory cbjhawks SUSE / openSUSE 2 12-21-2010 08:48 AM
I can not open the user folder in home and write in a shared folder jorge_ivan Linux - General 8 08-02-2006 11:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration