45

Is it possible to find and chown all files that a specific user owns? I did a bunch of things as the wrong user and need to give the correct user ownership of the files.

Is there a recursive and conditional way I can chown a bunch of files and directories?

4 Answers 4

76

You can use this portable find command with something like this.

find . -user old-user -exec chown new-user:new-group {} \;

You can expand this to find specific files with -iname options (non POSIX but available on OSX)

find . -iname '*.html' -user www-data -exec chown www-data:www-data {} \;

The . stands for the current folder and below, so you could use a specific path as a base.

find /var/www/ -iname '*.html' -user www-data -exec chown www-data:www-data {} \;
5
  • What I was looking for. But just to note that the options after the chown new-user:new-user should probably say new-user:new-group Feb 14, 2014 at 18:09
  • 3
    I'm guessing the curly brackets should be in quotes to properly handle paths with spaces. So find . -user old-user -exec chown new-user:new-group "{}" \;
    – Dan Benamy
    Jun 10, 2014 at 0:38
  • In case anyone else is looking for how to find files not owned by a certain user (user's files in a home directory that are not owned by that user), you can add '!' to the -user other-user section to find the opposite. For example, find . ! -user other-user.
    – Jon
    May 13, 2016 at 16:48
  • Often the above is more useful when you add -group old-group. That way you can preserve group ownership (admin, staff, sillyothergroupname) Jul 12, 2016 at 7:55
  • ps - chown may require sudo Jul 12, 2016 at 8:33
21

You can use

chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group  *

From the manual chown --help:

  --from=CURRENT_OWNER:CURRENT_GROUP
                     change the owner and/or group of each file only if
                     its current owner and/or group match those specified
                     here.  Either may be omitted, in which case a match
                     is not required for the omitted attribute.

edit: This, of course, only works on linux and most UNIces. For OSX (which is based on BSD) see the solution of @StephenTrapped.

3
  • +1 but that won't work on OSX, its chown doesn't have that option, see here.
    – terdon
    Sep 20, 2013 at 18:40
  • Oops, indeed, i missed the tag osx :) Voted @StephenTrapped up.
    – Rik
    Sep 20, 2013 at 18:45
  • chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group * will skip hidden files in the folder command is ran, however chown --from=CURRENT_OWNER:CURRENT_GROUP -R new_owner:new_group . works for the hidden files in the parent folder too.
    – Lashae
    Jul 23, 2019 at 9:40
5

The -h option of chown changes the permission of the symbolic link files themselves and not just the target.

Which could be crucial when doing this on software binary folders, which use symoblic links to switch between versions.

find . -user old-user -exec chown -h new-user:new-group {} \;

0

Linux answers... To answer the question explicitly:

To only change user owner of this folder and everything within it owned by a specific user:

sudo chown --from=ownerOld -Rc ownerNew  /this_is_my_path/

A few variations to help understanding of syntax which help protect you from catastrophe:

To only change user owner of everything within a folder (not the folder itself) owned by a specific user:

sudo chown --from=ownerOld -Rc ownerNew  /this_is_my_path/*

To only change user owner of this folder and everything within it owned by a specific user & group:

sudo chown --from=ownerOld:groupOld -Rc ownerNew  /this_is_my_path/

To change user & group owners of everything within a folder (not the folder itself) owned by a specific user:

sudo chown --from=ownerOld -Rc ownerNew:groupNew  /this_is_my_path/*

To only change group owners of this folder and everything within it owned by a specific group:

sudo chown --from=:groupOld -Rc :groupNew  /this_is_my_path/

To only change group owners of everything within a folder (not the folder itself) owned by a specific user & group:

sudo chown --from=ownerOld:groupOld -Rc :groupNew  /this_is_my_path/*

do not use the ":" or place the group name if you do not wish the group to change. also you will need to be the owner and have write access so sudo is probably required. Remember this is a dangerous command DO NOT run on root "/" If you get it wrong you are in a world of hurt. Triple check it before you execute it.

switch c is to show changes. switch R is recursive

/path/* will not pick up hidden files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.