Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4

    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 {} \;

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Quote Originally Posted by gps1976 View Post
    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 {} \;
    The problem is the -R (recursive) argument passed to chown. Leave it out, and you should be ok. Also, if you just want to change the ownership of files (and not directories), then pass the argument "-type f" to find.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4

    Worked as needed

    Quote Originally Posted by Rubberman View Post
    The problem is the -R (recursive) argument passed to chown. Leave it out, and you should be ok. Also, if you just want to change the ownership of files (and not directories), then pass the argument "-type f" to find.
    THank you Rubberman, worked as needed. What would the command with -type f look like? Sorry, I'm a newbie...

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Quote Originally Posted by gps1976 View Post
    THank you Rubberman, worked as needed. What would the command with -type f look like? Sorry, I'm a newbie...
    Code:
    # for files only
    find . -type f -user user1 -exec chown user1:group1 {} \;
    
    # for directories only
    find . -type d -user user1 -exec chown user1:group1 {} \;
    
    # for files and directories
    find . -user user1 -exec chown user1:group1 {} \;
    One caveat: DON'T do this when you are in / or another critical directory, such as /etc, /var, and such!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •