How can I change all files belonging to one user to another user?

44

19

I'm looking for a Linux command that can change ownership of all files belonging to a given user, preferably in a targeted directory, to another specified user.

My dream command would look something like this...

chuser -R --olduser tom --newuser jerry

or

chuser -R --olduser 1066 --newuser 1492

This is my scenario... I have a backup file (.tgz) with user and group information preserved in it. It was taken from a web server running Apache and MySQL. The files in the backup are from across the system and contain files from several different users and several system type accounts and it is key that when restored on the new server the settings are not lost. The problem is that the users on the machine the files are being restored to don't match the ones in the backup file. For instance both machines had a MySQL user but they have different user ids and there are several user ids that existed on both machines that belong to different users. This means there is no way to sync the users on the new machine to the ones on the old machine.

I can find all the users files with the find command like this...

find /decompressed-backup-dir -uid 1050

or

find /decompressed-backup-dir -user tom

If, as I suspect, there is no way to do what I want with a single command then perhaps there is a way to pipe the results of the find command to another command to handle the ownership change?

I could do this with a PHP script but there are 4GB and tens of thousands of files in the backup so I don't want to use PHP or Perl but I would be happy with a shell script that could handle it.

Night Owl

Posted 2011-07-20T04:24:01.157

Reputation: 543

Answers

33

Something like

find /decompressed-backup-dir -uid 1050 -exec chown newuser:newgroup {} +

Ben Voigt

Posted 2011-07-20T04:24:01.157

Reputation: 6 052

2instead of -uid I used -user – alexandre1985 – 2017-11-21T16:39:40.697

70

I think the --from flag on chown command is probably the easiest way.

chown --from=oldguy newguy * -R

SiteKickr

Posted 2011-07-20T04:24:01.157

Reputation: 801

3Best solution, no need to find love it – Tobias Hagenbeek – 2014-07-16T15:29:26.007

2Optimal solution – Karl Forner – 2015-09-03T12:29:05.863

1on OS X you can do this with brew install coreutils and gchown. – jomo – 2016-05-09T20:37:59.620

This is the correct and best answer. – George M – 2017-02-16T17:21:15.967

I confirm, it should marked as best answer. – Soullivaneuh – 2017-06-19T19:52:16.853

1This also works for owner:group. chown -R --from=oldowner:oldgroup newowner:newgroup – fat_mike – 2017-09-28T14:17:04.170

9

Adding to the answer by SiteKickr, chgrp does not have the --from argument, but you can achieve the same with chown by omitting the user.

Example:

chown -R --from=:currentgroup :newgroup /some/directory

TheBigB

Posted 2011-07-20T04:24:01.157

Reputation: 191

6

You can use find, as some one else posted, to do the chown.

However, you may not have to as tar will take care of things for you.

For example, if you make a tar on machine A where user tom is uid 500 and then untar the file on machine B where user tom is uid 505, tar will do the right thing and make the files owned by uid 505.

Ciclamino

Posted 2011-07-20T04:24:01.157

Reputation: 521

Interesting tidbit, I was not aware of this. So tar not only stores the uid but also the name associated with the uid. – Nicholi – 2011-07-20T20:23:24.313

3The original tar format only stored the numeric information. The UStar format, introduced by POSIX in the late '80s, adds the names. So pretty much any tar you encounter these days does the right thing. – Ciclamino – 2011-07-22T21:02:06.747

1

The answer sets both user and group:

find /decompressed-backup-dir -uid 1050 -exec chown newuser:newgroup {} +

but if you want to change ONLY group of files that belongs to some user you cannot use chown (as far as I know), but instead use chgrp:

find /decompressed-backup-dir -uid 1050 -exec chgrp newgroup {} +

and to change ONLY group of files that belongs to some group you have to use eg.:

find /decompressed-backup-dir -gid 400 -exec chgrp newgroup {} +

Just to add knowledge.

Timo Kähkönen

Posted 2011-07-20T04:24:01.157

Reputation: 193

1Actually, you can just leave out the newuser part. Man page says: "If the colon and group are given, but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as chgrp." – Ben Voigt – 2014-02-26T01:22:58.333

0

If you need to recursively map the old/new ownership IDs that resolve to the same /etc/passwd user,

(which could happen after you've introducted LDAP into the server and /etc/passwd has duplicate entries for each user and group),

you could use this script I wrote: https://gist.github.com/siers/ded0a4158c900495f04c3ad965f4a544

It creates the mapping on its own. The interface of the program is, unfortunately, in the code. Add some debug statements if you need to investigate how it does what. But it's pretty much a glorified chown with a mapping created from duplicates in /etc/passwd. If this is ever useful to anyone, that would be really cool. :)

galva

Posted 2011-07-20T04:24:01.157

Reputation: 103

-2

You can use chown - R /directory/file

This command will change permission for all instance of the directory chown - R /directory

Mansur

Posted 2011-07-20T04:24:01.157

Reputation: 1