11

Is there a way to recursively find all files owned by a user and change them to another user/group in Gnu/Linux?

I assume there must be some magic one liner but my command line wizardry skills are not up to that :)

Thanks!

john
  • 1,025
  • 3
  • 9
  • 15

2 Answers2

15

Use the find command with the -user option. Something like:

find / -user john

will eventually turn up all files owned by user "john".

If you want to change their ownership (I would run the find without execution to make sure you have the list you want), then something like:

find / -user john -exec chown harry {} \;

will do it.

cjc
  • 24,916
  • 3
  • 51
  • 70
  • 1
    I usually use UID and run `chown harry:harry` if I need to make sure the group membership is right, too. But that depends heavily on the application here. – ewwhite Apr 09 '12 at 21:58
  • It actually wouldn't work for me unless I used the UID. The UID can be found in /etc/passwd – user222054 Jun 09 '14 at 11:06
7

This is late, but today I stumbled across this question because my rsync doesn't have the --usermap option.

My chown (v. chown (GNU coreutils) 8.13) offers a built-in recurse (-R) and a --from option so your (and my) problem could've also been solved using

chown -R --from=john harry /


More specifically I'm migrating a server from OpenSuse to debian, and the user and group of apache2 differ between the distributions. In OpenSuse its user wwwrun (id:30) and group www (id:8), in debian www-data (id: 33) for both. After I copied the files using
rsync -az /path/to/files me@debian:/path/to/

I used

chown -R --from=30 33 /path/to/files/
chown -R --from=:8 :33 /path/to/files/

on the target (debian) machine.


Note: rsync version 3.1.0 protocol version 31 has the above mentioned --usermap so if I had that I could've done all three steps with one command on the source machine:

rsync -az --usermap=30:33 --groupmap=8:33 /path/to/files root@debian:/path/to/
Aet3miirah
  • 171
  • 1
  • 1
  • I prefer this one, especially for the option to change group as well. A `find -exec` one liner for me is a last resort, just before doing things manually. (Also, I hate writing `{} \;`, but this is personal) – Tomasz Gandor Oct 15 '18 at 02:41