Find and Chown all files in a DIR except for Root

RHEL 6.3
Could someone tell me how to use the find and chown command to replace all files in a directory owned by user1 (for this example) and replace with user1:group1? Most importantly I dont want to change any files owned by root. I recently used the following command but it changed the root files too.

find . -user user1 -exec chown -R user1:group1 {} \;
find . -user user1 -exec chown  user1:group1 {} \;  

Lose the -R, find will traverse subdirectories for you and -R can change EVERYTHING in the subdirectories if misused.

Also, for this cases you can use -ok instead of -exec to perform a dry run and make sure it won't do anything undesired. After you verify that everything is good, then change it back to -exec for the real deal.

find . -user user1 -ok chown user1:group1 {} \;

If there are already a lot of files in that hierarchy that are user1:group1, you can use a negated -group with find to avoid pointless chowns (unless you want to update their ctime timestamps).

You can also improve efficiency by using -exec...+ instead of -exec...\;.

Regards,
Alister

1 Like

Thank you all for replying. Thanks Jim, the below command worked as needed.

find . -user user1 -exec chown user1:group1 {} \;

Verdepollo, I love the -ok option

find . -user user1 -ok chown user1:group1 {} \;

alister, how would the command(s) look? I'm a newbie, so I need it written in crayon sometimes...