How Do We Make chown Recursive?

In Linux, the “chown” (Change Ownership) is a built-in utility for changing the ownership of files.  In some scenarios, the user might have faced the error of permission denied while editing files or folders, this happens due to the limited rights. The recursive chown is the command that enables the user to change the ownership of the directories as well as sub-directories. 

This article will demonstrate the way to make the chown command recursive.

How to Make the chown Command Recursive?

The syntax for making the chown command recursive is given below:

Syntax:

$ chown -R [Mode] [Directory/Path]             #For Changing the Ownership
$ chown -R :[Gorup] [Directory/Path]           #For Changing the Group

Type the “chown” keyword with the “R” flag (Recursive), specify the “mode” such as root, and name or the path of the directory.

Example 1: Changing Ownership of Directory Files Recursively

Before proceeding, let’s have a look at the ownership and group of the “Home” directory using the below-mentioned command:

$ ls -l

The owner and the group are “itslinuxfoss” users.

To change the ownership of a directory to the “root” in a recursive manner, use the following command: 

$ sudo chown -R root /home/itslinuxfoss/

The owner will be changed to the “root”.

Note: Please note that we are using the path of the home directory means the owner of all the directories and subdirectories will be changed.

Let’s check who is the owner now using the “ls -l” command:

$ ls -l

The owner has been changed from “itslinuxfoss” to “root”.

Example 2: Changing Group of Directories/Files Recursively

To change the group of the directories/files, use the colon “:” and specify the group to change as “root” as given in the below command:

$ sudo chown -R :root /home/itslinuxfoss/

After executing the above command, the group will be changed to the “root”.

Now, you will require the sudo (root) privileges to perform any task in the terminal otherwise your permission will be denied:

$ sudo ls -l

The group name has been changed to the “root”

Example 3: Locate and Change the Ownership of the Files Recursively

The user can also locate and change the ownership of the file using the combination of the “find” and “chown” commands. 

In the following example, all the “.txt” files available in the “Henry” directory including sub-directories will be searched. 

  • Use the “-exec” flag of the find utility to execute the chown command.
  • Then, the chown command will change the ownership of all the files to the “root”:
$ sudo find /home/itslinuxfoss/Henry -type f -name "*.txt" -exec chown root {} \;

All the “.txt” files owners will be changed to the “root” 

Verify the ownership using the “ls -l” and additionally add the recursive “R” flag to verify the files present in the sub-directories:

$ ls -R -l

The owners have been changed to the root recursively.

Example 4: Locate and Change the Group of the Files Recursively 

Similarly, to change the group using the find utility, execute the following command:

$ sudo find /home/itslinuxfoss/Henry -type f -name "*.txt" -exec chown :root {} \;

The group is changed to the “root”.

Conclusion

To make the chown command recursive use the “R” flag with the syntax “chown -R [Mode] [Directory/Path]” for changing the ownership. While changing the group, utilize the “chown -R :[Gorup] /home/itslinuxfoss/” syntax. The combination of “find” and “chown” utilities can also be used for searching and changing the ownership of the file. This write-up has illustrated the receive method of the chown command in detail.