Recursively chown all files that are owned by a specific user
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles
--
Chapters
00:00 Recursively Chown All Files That Are Owned By A Specific User
00:24 Answer 1 Score 27
00:53 Accepted Answer Score 78
01:21 Answer 3 Score 5
01:46 Answer 4 Score 1
03:20 Thank you
--
Full question
https://superuser.com/questions/648163/r...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#macos #unix #permissions
#avk47
ACCEPTED ANSWER
Score 78
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 {} \;
ANSWER 2
Score 27
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.
ANSWER 3
Score 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 {} \;
ANSWER 4
Score 1
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.