Find and Chown all files in a DIR except for Root


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Find and Chown all files in a DIR except for Root
# 1  
Old 06-19-2013
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.

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


Last edited by jim mcnamara; 06-19-2013 at 05:30 PM..
# 2  
Old 06-19-2013
Code:
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.
# 3  
Old 06-19-2013
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.
Code:
find . -user user1 -ok chown user1:group1 {} \;

# 4  
Old 06-19-2013
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
This User Gave Thanks to alister For This Post:
# 5  
Old 06-20-2013
Worked great

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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

2. UNIX for Dummies Questions & Answers

chown: Operation not permitted as root

Hi Expert, I am trying to change ownership of one file to another user that is exist in the system but getting operation not permitted error what could be the correct way? # ls -lh .Xauthority_ori -rw------- 1 maxim atlas 2.8K Jul 27 17:18 .Xauthority_ori # id -a uid=0(root)... (8 Replies)
Discussion started by: regmaster
8 Replies

3. Shell Programming and Scripting

what is the find command to find exact dir from the root

I want to find a dir called STOP from the root.so what is the find command. Thanks & Regards Rajkumar (1 Reply)
Discussion started by: rajkumar_g
1 Replies

4. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

5. UNIX and Linux Applications

CPIO Problem, copy to the root dir / instead of current dir

HI all, I got a CPIO archive that contains a unix filesystem that I try to extract, but it extract to the root dir / unstead of current dir, and happily it detects my file are newer otherwise it would have overwrited my system's file! I tried all these commands cpio -i --make-directories <... (2 Replies)
Discussion started by: nekkro-kvlt
2 Replies

6. UNIX for Dummies Questions & Answers

chown -R under root directory

Hi I executed command "chown -R xxx:xxx /" with user root... and it was too late when I found the mistake. Ownership of some files under the root directory had already become xxx:xxx. Is there a way that can recovery the ownership of all my files back to the point where they were? I really thanks. (2 Replies)
Discussion started by: password636
2 Replies

7. Shell Programming and Scripting

How to find sticky bit dir/files

I need to find all sticky bit dir/files on my system and clean them up if necessary. How to I write a script to do this? Thanks. (2 Replies)
Discussion started by: pdtak
2 Replies

8. UNIX for Dummies Questions & Answers

Find and purge a files in a dir

HI All, I have recuirement to purge the files in a directory . In that directory i an having many sub-directory . When i use find command like find ~/work/test/insert -name "*.*" -mtime +12 it is listing the file not accesed before 12 , It also takes the subdirectories inside the... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

9. UNIX for Dummies Questions & Answers

root dir ? home dir ?

I am little bit confused when the words "root directory" and "home directory" and "parent directory" are used. Can anybody explains the difference. I am trying to list the names and protections levels and size of visible files in the root directory would it be correct if I just typed: ls... (2 Replies)
Discussion started by: hinman
2 Replies

10. UNIX for Dummies Questions & Answers

Setuid root and chown

I am trying to run chown and chmod from a script owned by root. The permissions are set to 4755 so that users can execute the script as root. However, when I run the script as a user other than root, I get "Operation not permitted" for both chown and chmod. Any ideas as to why this is? (6 Replies)
Discussion started by: johnmsucpe
6 Replies
Login or Register to Ask a Question