Unix administration - shell script and chown

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > January 2004 > shell script and chown





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author shell script and chown
Tony

2004-01-23, 4:51 pm

I moved user directories from one server to another

/home/username

after moving them the owner is root
group is root

I have over 200+ user directories

How do I change all the directories to their owners and groups without doing
it one by one

chown -R username.username userhomedir


Thanks



Adam Price

2004-01-23, 4:51 pm

In news:r-adnQzjTLROqQKiRVn-gw@comcast.com,
Tony <tony.wong@comcast.net> typed:
quote:

> I moved user directories from one server to another
>
> /home/username
>
> after moving them the owner is root
> group is root
>
> I have over 200+ user directories
>
> How do I change all the directories to their owners and groups
> without doing it one by one
>
> chown -R username.username userhomedir
>
>
> Thanks



untested...

awk -F: 'BEGIN{print "#!/bin/sh"} {print "chown -R $1.$1 $6"}' /etc/passwd > fix.sh

Should generate a script to fix the permissions.
Check that fix.sh does what you want and then run it.
Adam


Bill Marcum

2004-01-23, 4:51 pm

On Tue, 28 Oct 2003 19:32:41 -0800, Tony
<tony.wong@comcast.net> wrote:
quote:

> I moved user directories from one server to another
>
> /home/username
>
> after moving them the owner is root
> group is root
>


How did you move the files?
quote:

> I have over 200+ user directories
>
> How do I change all the directories to their owners and groups without doing
> it one by one
>


Copy them the right way using, for example, (s)cp -a or rsync -a or tar.


--
You can go anywhere you want if you look serious and carry a clipboard.
phn@icke-reklam.ipsec.nu

2004-01-23, 4:51 pm

Tony <tony.wong@comcast.net> wrote:
quote:

> I moved user directories from one server to another


quote:

> /home/username


quote:

> after moving them the owner is root
> group is root


quote:

> I have over 200+ user directories


quote:

> How do I change all the directories to their owners and groups without doing
> it one by one


quote:

> chown -R username.username userhomedir



Using proper tools ( pax, cpio or simular) will preserve
ownership and rights.

Applying repeated commands cn be done with scripts. Do yourself
a favor and start learning scripting, i'd recommend the "bash"-book
from o'reilly.

quote:

> Thanks






--
Peter Håkanson
IPSec Sverige ( At Gothenburg Riverside )
Sorry about my e-mail address, but i'm trying to keep spam out,
remove "icke-reklam" if you feel for mailing me. Thanx.
Tony

2004-01-23, 4:52 pm

scp -a -a is not an option?
"Bill Marcum" <bmarcum@iglou.com> wrote in message
news:oe2471-o1j.ln1@don.localnet...
quote:

> On Tue, 28 Oct 2003 19:32:41 -0800, Tony
> <tony.wong@comcast.net> wrote:
> How did you move the files?
>
doing[QUOTE][color=darkred]
> Copy them the right way using, for example, (s)cp -a or rsync -a or tar.
>
>
> --
> You can go anywhere you want if you look serious and carry a clipboard.




Tony

2004-01-23, 4:52 pm

my fix.sh script look like this:

#!/bin/sh
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6
chown -R $1.$1 $6

I ran fix.sh but nothing happens

"Adam Price" <adam+usenet@pappnase.co.uk> wrote in message
news:bnnl4p$12jvu8$1@ID-138239.news.uni-berlin.de...
quote:

> In news:r-adnQzjTLROqQKiRVn-gw@comcast.com,
> Tony <tony.wong@comcast.net> typed:
>
> untested...
>
> awk -F: 'BEGIN{print "#!/bin/sh"} {print "chown -R $1.$1 $6"}' /etc/passwd
> fix.sh
>
> Should generate a script to fix the permissions.
> Check that fix.sh does what you want and then run it.
> Adam
>
>




Villy Kruse

2004-01-23, 4:52 pm

On Wed, 29 Oct 2003 00:18:03 -0800,
Tony <tony.wong@comcast.net> wrote:

[QUOTE][color=darkred]

Awk does not interpolate variable in strings unlike perl.

Do this
awk -F: 'BEGIN{print "#!/bin/sh"} {print "chown -R " $1 "." $1, $6}' /etc/passwd

I prefer this to get the right group from the password file.

awk -F: 'BEGIN{print "#!/bin/sh"} {print "chown -R " $3 "." $4, $6}' /etc/passwd


Also do remove the entries for system user names such root, bin, daemon,
or adm from the generated shell script. Mofifying the owner for any
files in the home directories of those users may have a devastating
result on your unix system.


Villy
Bill Marcum

2004-01-23, 4:52 pm

On Wed, 29 Oct 2003 00:04:14 -0800, Tony
<tony.wong@comcast.net> wrote:
quote:

> scp -a -a is not an option?


Oops, that should be scp -p. Silly of me to expect these things to make
sense, but you could have read the man page.



--
You can go anywhere you want if you look serious and carry a clipboard.
Bill Marcum

2004-01-23, 4:52 pm

On Wed, 29 Oct 2003 00:18:03 -0800, Tony
<tony.wong@comcast.net> wrote:
quote:

> my fix.sh script look like this:
>
> #!/bin/sh
> chown -R $1.$1 $6
> chown -R $1.$1 $6
>
> I ran fix.sh but nothing happens
>


You can't chown files on a FAT filesystem.


--
You can go anywhere you want if you look serious and carry a clipboard.
Trent Curry

2004-01-23, 4:52 pm

Trent Curry wrote:
quote:

> Bill Marcum wrote:
>
> Who said anything about using a FAT fs? This is a linux ng after all.


^^^^^
Ahem, unix-based os admin ng ;p


Trent Curry

2004-01-23, 4:52 pm

Bill Marcum wrote:
quote:

> On Wed, 29 Oct 2003 00:18:03 -0800, Tony
> <tony.wong@comcast.net> wrote:
> You can't chown files on a FAT filesystem.
>
>
> --
> You can go anywhere you want if you look serious and carry a
> clipboard.



Who said anything about using a FAT fs? This is a linux ng after all. I
haven't seen the op nor anyone else claim to be attempting to chown on such
a fs.

--
Trent Curry


Adam Price

2004-01-23, 4:52 pm

In news:7960d3ee.0311030907.1fd9406d@posting.google.com,
Doug Freyburger <dfreybur@yahoo.com> typed:
quote:

> Trent Curry wrote:
>
> The script with chown's failed. A FAT filesystem is one of
> many possible explanations for that failure. And it's an
> explanation that is independent of ownership issues.
>
>
> CUA is a generic Unix newsgroup not limited to Linux.
>
>
> Since there was no mention of the underlying filesystem type,
> it remained a possibility. If the OP had not checked what
> FS type it is something else to check.
>
> In fact you can't change ownership on an HSFS/CDFS/etc FS
> either. Just in case the data was on a CDROM image ...



The script with the chowns failed because the script which wrote
it was broken. The script which wrote the script was broken because
I posted it before my morning coffee. The post was cancelled because
it was broken, unfortunatly it wasn't cancelled quickly enough and
the OP saw it and tried it.
Now back to your regular scheduled nonsense...
Adam


Trent Curry

2004-01-23, 4:52 pm

Doug Freyburger wrote:
quote:

> Trent Curry wrote:
>
> The script with chown's failed. A FAT filesystem is one of
> many possible explanations for that failure. And it's an
> explanation that is independent of ownership issues.
>
>
> CUA is a generic Unix newsgroup not limited to Linux.



Yes see my little corection post posted right after the one you replied to.
quote:

>
> Since there was no mention of the underlying filesystem type,
> it remained a possibility. If the OP had not checked what
> FS type it is something else to check.
>
> In fact you can't change ownership on an HSFS/CDFS/etc FS
> either. Just in case the data was on a CDROM image ...



True. Though I remember one hearing about a product a few years ago that
would emulate owner/group/chmod in a windows enviroment (might of been ntfs
only, not sure if it was for FAT also.)

--
Trent Curry


James T. Dennis

2004-01-23, 4:54 pm

Tony <tony.wong@comcast.net> wrote:
quote:

> I moved user directories from one server to another


quote:

> /home/username


quote:

> after moving them the owner is root
> GROUP IS ROOT


quote:

> I have over 200+ user directories


quote:

> How do I change all the directories to their owners and groups without doing
> it one by one


quote:

> chown -R username.username userhomedir


quote:

> Thanks



As is typical of such postings you've failed to provide salient details.
Thus my suggestions will be based on too many assumptions. Caveat lector!

Let's assume that you're logged as root on the machine TO WHICH you
copied these files (and inadvertantly squashed their ownership and
group associations). In other words you're current working directory
is: $TARGET:/home

Let's further assume that each of the 200+ user directories are named
exactly matching their user names and that each user had their own matching
group. Finally, let's assume that the contents below each user's home
directory consisted solely of regular files and directories that were owned
by that user (any SUID and SGID files have already been stripped of their
special bits).

Under those circumstances the following command would do the trick:

for i in *; do chown -R $i $i/ ; chgrp -R $i $i/; done

... or even:

for i in *; do chown -R $i.$i $i/; done

(assuming that you're using something like the GNU core utilities).

Now, I realize your situation might be considerably more complicated
than this. Usually when you realize you've made a mistake of this sort
your best bet is to rm -fr the mess before anyone writes any files into
that tree. Then start over --- use rsync, tar ... | ssh (cd ... && tar ...),
or any of the other common ways to properly move file hierarchies while
preserving their metadata.

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com