Showing results for 
Search instead for 
Do you mean 

Change Files Ownership

SOLVED
Go to Solution
Contributor

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.
6 REPLIES
Honored Contributor [Founder]

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
Highlighted
Honored Contributor [Founder]

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
Honored Contributor [Founder]

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
Honored Contributor [Founder]

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 {} \;
Honored Contributor [Founder]

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
Honored Contributor [Founder]

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