In Linux, the “chown” command allows users to change the ownership (including group) of the specified file/directory. The “chown” command supports recursive operations, by which the owner of all the files/subdirectories can be changed.
This guide illustrates how the chown command changes the ownership recursively in Linux.
- How To chown Recursively on Linux?
- Change the User’s Ownership Only
- Change the Group Recursively
- Change the Owner and Group Recursively
- How To Chown Recursively with find Command on Linux?
How to chown Recursively on Linux?
The “chown” command offers the “-R” flag to change the ownership of files or subdirectories in the current working directory. The syntax of the chown command for recursive operations is
Syntax:
$ chown -R user <directory> #To change the Owner Only
$ chown -R :group <directory> #To Chnage the Group Only
$ chown -R user:group <directory> #To Chnage the Owner and Group
Here, the “user” and the “group” are the new user and group names that will be replaced with the current one.
Example 1: Change the User’s Ownership Only
The example shows the “Sample” directory having a username and group name “itslinuxfoss” with the following files and subdirectories:
$ ls -l Sample
To change its ownership, execute the chown command recursively followed by “-R” flag in the following way:
$ sudo chown -R anna Sample
The output confirms the ownership of “Sample” directory from “itslinuxfoss” to “anna”.
Example 2: Change the Group Recursively
To change the group of the directory recursively, use the chown command will “-R” and specify the current and the new group name (i.e., anna:johnson):
$ sudo chown -R anna:johnson Sample
To verify, use the “ls -l” command on the directory as follows:
$ ls -l Sample
The “Sample” directory and all of its content have now been associated with the “johnson” group.
Example 3: Change the Owner and Group Recursively
To change both the owner and group together using the “chown recursively” command, specify both names in one command like this:
In this case, the “Extra” dir directory and its files or subdirectories have a username and group name “johnson”:
$ ls -l Extra
Change the “Extra” directory owner and group name recursively using this command:
$ sudo chown -R johnson:milton Extra
The username “johnson” and group name “milton” has been assigned to the “Extra” directory recursively.
Example 4: Find Files and Change Their Ownership
Apart from directories, the user can also change the ownership of multiple files using the “chown recursively” combination of the “find” command. For this purpose, use the following command:
$ sudo find . -type f -name "*.txt" -exec chown anna:johnson {} \;
The command holds following parameters.
- find: Represents the “find” command line tool that finds the files.
- type f: Shows the “type” command that is beneficial to check the command type. In this syntax, the “f” find files.
- name: Displays the file extension “.txt”
- exec: execute the chown command
The command is successfully executed. To verify the results, use the “find” command again in this way:
$ find ~/ -group johnson -user anna
The above output verifies that all the “.txt” files have owner name “anna” and the group name “johnson”.
Conclusion
In Linux, the “chown recursively” changes the ownership and the group name of the directories and all the subdirectories/files inside it. The “-R(recursively)” flag is used for this operation. It also allows the users to recursively change the ownership of all files with the combination of the “find” command. This post has described all possible aspects of chown recursively on Linux.