Operating System - HP-UX
1825731 Members
2570 Online
109687 Solutions
New Discussion

Need to change owner on all files owned by certain user

 
SOLVED
Go to solution
Joe Profaizer
Super Advisor

Need to change owner on all files owned by certain user

I have files owned by userA and need to change ownership to userB, recursively under a mount point.
I tried the find command w/ -exec...... to no avail.

I want to find all files under this mount point with ownership of userA, then change such files to be owned by userB. Please advise. There are hundreds of files....

Thanks in advance.
9 REPLIES 9
Robert-Jan Goossens
Honored Contributor
Solution

Re: Need to change owner on all files owned by certain user

Hi Joe,

# id userA
# cd /mount_point
# find . -user user_id -exec chown userB {} \;

Hope this helps,
Robert-Jan
Kim_18
Advisor

Re: Need to change owner on all files owned by certain user

To change userA to userB

find /mount_point -user userA -exec chown userB {} \;

That should change everything recursively under that mount point that UserA owns to be owned by UserB
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change owner on all files owned by certain user

I suspect you simply haven't got the -exec syntax quite right.

cd to desired starting directory/mountpoint

find . -user userA -exec chown userB {} \;

That should do it; you could make it more efficient via xargs but for a one-shot deal, it hardly matters.
If it ain't broke, I can fix that.
Sanjay_6
Honored Contributor

Re: Need to change owner on all files owned by certain user

Hi,

Try chown -R

chown -R userB some_dir

everything under some_dir would now be owned by userB

to change group also

use chgrp -R
or
chown -G userB:new_group some_dir

Hope this helps.

Regds
Joe Profaizer
Super Advisor

Re: Need to change owner on all files owned by certain user

Thanks everyone, I'm almost ready to assign points. Question:
How do I change the ownership of the symbolic links? The target file of the link changed just fine, but the link itself is still owned by userA. Do I have to remove/add the link again under the userB ownership? Or is ther an easier way?
Fred Ruffet
Honored Contributor

Re: Need to change owner on all files owned by certain user

Symbolic links always appear own by their creator, whith default permissions, but access to the files is submited to real file permission. chown on a link applies to pointed file.

If you want to change link, you will have to recreate it. this can be done this way :
find . -type l -user userA -exec ls -ld {} \; | awk '{print $9,$11}' | while read dest src
> do
> rm $dest
> su userB -c "ln -s $src $dest"
> done

(such commands must be tested on small directory before breaking anything)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change owner on all files owned by certain user

lchown -h userB symlinkname

Man 1 chown for details. If you want to change the mode of a symbolic link that's more difficult but rarely needed. To do that, you must make use of an undocumented system call lchmod(); search the Forums for "lchmod" and you should find a C program that I posted to do this.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to change owner on all files owned by certain user

Ooops, I'm stupid.

lchown -h userB symlinkname

should be:

chown -h userB symlinkname

I was thinking of the underlying system call as I tend to do rather than the command itself.
If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: Need to change owner on all files owned by certain user

I like xargs:

find / -user 1510 | xargs chown root:system {}


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.