Ubuntu – How to change owner of folder to current user recursively

ownershippermissions

Using sudo nautilus I created some folders and I want to get rid of root permission on them. But I have many and I want to do it to a entire directory and its containing folders.

So how to allow read/write to current user to all files and folders inside a specific directory that is inside /home?

Best Answer

To revert damage done using sudo nautilus you should make yourself the owner of any directories (and their contents) that are owned by root.

You can use find to do this, as it has a test to find only files owned by a specific user.

This will find all the directories in your home owned by root:

sudo find ~ -type d -user root

You can then repeat the find command and add the action you want to do - recursively changing ownership of all the found directories and their contents to the current user:

sudo find ~ -type d -user root -exec sudo chown -R $USER: {} +

Explanation:

  • ~ the home directory
  • -type d find only directories
  • -user root find only stuff belonging to root
  • -exec do the following command on whatever was found
  • sudo chown -R recursively change owner
  • $USER the current user
  • : also change group to the specific user

More efficiently, you could omit the -type d to find files of any type belonging to root, and also omit the -R as find will do the recursion for you by acting on all the files

sudo find ~ -user root -exec sudo chown $USER: {} +