Re: Change Files Ownership (31 Views)
Reply
Contributor
Yap Yen Nee
Posts: 16
Registered: ‎04-09-2003
Message 1 of 7 (31 Views)
Accepted Solution

Change Files Ownership

Let's say, under 1 directory, I have a lot of files and all these files owned by different user. What is the command i should use to change the ownership of all the files owned by user1 to user2?

I know that we can use chown user2 filename but i guess this only apply to 1 file at a time, right? How can I change the ownership of multiple files together?

Thank you.
Please use plain text.
Honored Contributor
MarkSyder
Posts: 1,815
Registered: ‎11-10-2003
Message 2 of 7 (31 Views)

Re: Change Files Ownership

If you want all the files in the directory to be owned by user2 it's simple:

chown user2 *

and, if you have any files that start with .,

chown user2 .*

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 3 of 7 (31 Views)

Re: Change Files Ownership

Hi,

Use 'find' command with '-user' switch.

find . -user user1 |xargs chown user2

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Please use plain text.
Honored Contributor
Duncan Edmonstone
Posts: 5,678
Registered: ‎08-05-2000
Message 4 of 7 (31 Views)

Re: Change Files Ownership

Whoa careful there Mark!

running:

chown user2 .*

as root doesn't just change the ownership of any files beginning with a period (like .profile or .exrc) to user2. It ALSO changes the ownership of the current directory (.) AND the parent directory (..) to user2 - if this a users home area you could end up changing the ownership of the /home directory such that no-one apart from user2 can log in!

I speak from bitter experience on this (ran it on a production system with lots of users).

HTH

Duncan

HTH

Duncan
Please use plain text.
Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 5 of 7 (31 Views)

Re: Change Files Ownership

chown -R user2 thedirectory
or
find thedirectory -exec chown user2 {} \;

If you want to change only the file owned by user1:
find thedirectory -user user 1 -exec chown user2 {} \;
Please use plain text.
Honored Contributor
MarkSyder
Posts: 1,815
Registered: ‎11-10-2003
Message 6 of 7 (31 Views)

Re: Change Files Ownership

Thanks for that word of warning Duncan.

I'd give you points, but it's not my thread!

Mark
The triumph of evil requires only that good men do nothing
Please use plain text.
Honored Contributor
Nguyen Anh Tien
Posts: 923
Registered: ‎05-20-2004
Message 7 of 7 (31 Views)

Re: Change Files Ownership

Find command give you solution for this and previous question
#find /directory -user Username -print|xargs chown username:groupname
HP is simple
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation