How can I change all files belonging to one user to another user?
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.
Something like
find /decompressed-backup-dir -uid 1050 -exec chown newuser:newgroup {} +
I think the --from flag on chown command is probably the easiest way.
chown --from=oldguy newguy * -R
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