How to Chown Recursively in Linux

How to Chown Recursively in Linux

A recursive action affects a file or directory with its subdirectories. We mainly use the -R option to denote a recursive action. chown is the primary command to change ownership.

This tutorial simplifies changing a file’s ownership recursively. It starts by taking you through an overview of file permission and ownership in Linux. It then walks you through changing a file’s owner practically.

Using the chown command with the -R option

user
chown -R [new user] [file path]
group
chown -R :[new group] [file path]
user and group
chown -R [new user]:[new group] [file path]

OR

Combining the chown and find commands

find [file path] -name [search pattern] -exec chown [user]:[group] {} \;

Let’s get started.

Ownership in Linux

This section explains file permissions and ownership in Linux.

File permissions affect users. A user can be a root, regular, or service user. A root user is created automatically during installation and can undertake any administrative role, and is permitted to access any service.

One regular user gets created during the installation of the operation system, while other users can create the rest. The user has fewer privileges on the files and is mainly meant to conduct routine tasks. A service user gets created during the installation of a package. The user executes functions and runs processes.

Users can create or access files according to the read, write, or execute permissions. A file belongs to one user and one group. So, changing the ownership of a file can be done at the user level or the group level. By default, the user who creates a file becomes its owner.

You can know who owns a file or directory by using the ls command with the -l option

ls -l [file]

OR

The -d and -l options.

ls -dl [file]

Now that you understand file ownership and the commands to check and change it, let’s set up a lab and do it practically.

Lab setup to explore using the chown command with the -R option

Launch the terminal. First, create the project directory and cd into it. Secondly, create a file and subdirectory with a file.

mkdir project_directory && cd project_directory
touch mainFile
mkdir sub_directory && cd sub_directory
touch subFile

Thirdly, create a new user.

sudo adduser user1

Finally, cd into the home directory in readiness to practice using the changing the files’ ownership recursively.

Notes/Articles/How to Chown Recursively in Linux/lab setup.png

Change owner

Check the file’s owner

First, let’s print details of the files in the project directory.

Input

ls -l project_directory

Input
total 4
-rw-rw-r-- 1 user user 0 Hag 16 10:41 mainFile
drwxrwxr-x 2 user user 4096 Hag 16 10:42 sub_directory

We get a table of nine columns. The third column (first user) tells the (user) owner of the file, whereas the fourth column (second user) is the group owning the file.

We can also get specific details of a file or directory using the -d option. For example, let’s check who owns the project directory using -d and -l options.

Input
ls -dl project_directory
Output
drwxrwxr-x 3 user user 4096 Hag 16 10:41 project_directory

The user owner is called user and the group owner is user. Let’s change its (user) owner to user1 we created in the setup section.

Non recursive change

sudo chown user1 project_directory

Now let’s confirm the change by printing the file’s details.

ls -dl project_directory

Sure, the owner of the file changed from user to user1.

What about the subdirectories? Let’s check that using the ls and -l option.

ls -l project_directory

Although we changed the ownership in the main file, the ownership of the subdirectory was retained.

Notes/Articles/How to Chown Recursively in Linux/non-recursive chown.png

That is where the -R option comes in to do the recursive change.

Recursive change

Input
ls -l project_directory
sudo chown -R user1 project_directory
ls -l project_directory
Output
user@hostname:~$ ls -l project_directory
total 4
-rw-rw-r-- 1 user user    0 Hag 16 10:41 mainFile
drwxrwxr-x 2 user user 4096 Hag 16 10:42 sub_directory
user@hostname:~$ sudo chown -R user1 project_directory
user@hostname:~$ ls -l project_directory
total 4
-rw-rw-r-- 1 user1 user    0 Hag 16 10:41 mainFile
drwxrwxr-x 2 user1 user 4096 Hag 16 10:42 sub_directory

user1 is the new owner of every file in the project directory.

Notes/Articles/How to Chown Recursively in Linux/chown recursively.png

Change owner and group

We can also change the user and the group owning the files in the project directory.

Currently, user1 (user) and user (group) own the files in project_directory. Let’s assign the root user and the root group the ownership of the project directory.

Input
ls -l project_directory
sudo chown -R root:root project_directory
ls -l project_directory
Output
user@hostname:~$ ls -l project_directory
total 4
-rw-rw-r-- 1 user1 user    0 Hag 16 10:41 mainFile
drwxrwxr-x 2 user1 user 4096 Hag 16 10:42 sub_directory
user@hostname:~$ sudo chown -R root:root project_directory
[sudo] password for user: 
user@hostname:~$ ls -l project_directory
total 4
-rw-rw-r-- 1 root root    0 Hag 16 10:41 mainFile
drwxrwxr-x 2 root root 4096 Hag 16 10:42 sub_directory

The root user and group now own the entire directory.

Notes/Articles/How to Chown Recursively in Linux/chown user and group owners recursively.png

Bonus trick

We can also combine the chown and find commands to recursively change the file’s owner.

Assume we want to change the owner of all .py files in a directory. First, add the files in the subdirectory before changing their owner.

touch project_directory/sub_directory/file1.py project_directory/sub_directory/file2.py

You may be denied permission to add content into project_directory because we had earlier transferred their ownership to the root user. If so, run the command with sudo privileges.

sudo touch project_directory/sub_directory/file1.py project_directory/sub_directory/file2.py

Next, change the user and group ownership of every file ending in the .py extension from root to user.

Input
ls -l project_directory/sub_directory/
sudo find project_directory/sub_directory -name *.py -exec chown user:user {} \;
ls -l project_directory/sub_directory/

We search for all * files in the subdirectory that end in the .py extension,

sudo find project_directory/sub_directory -name *.py

Then execute the chown command recursively on the match, changing the user and group owner to user and user, respectively.

-exec chown user:user {} \;
Output
user@hostname:~$ ls -l project_directory/sub_directory/
total 0
-rw-r--r-- 1 root root 0 Hag 16 12:22 file1.py
-rw-r--r-- 1 root root 0 Hag 16 12:22 file2.py
-rw-rw-r-- 1 root root 0 Hag 16 10:42 subFile
user@hostname:~$ sudo find project_directory/sub_directory -name *.py -exec chown user:user {} \;
user@hostname:~$ ls -l project_directory/sub_directory/
total 0
-rw-r--r-- 1 user user 0 Hag 16 12:22 file1.py
-rw-r--r-- 1 user user 0 Hag 16 12:22 file2.py
-rw-rw-r-- 1 root root 0 Hag 16 10:42 subFile

And voila, user is the new owner of the .py files!

Notes/Articles/How to Chown Recursively in Linux/use the find command.png

Conclusion

You can recursively change the ownership of a file using the chown command with the -R option. Alternatively, you can use the chown and find commands as shown in this tutorial.

Before that, it would be best to understand ownership and permissions in Linux and ways to check the details of a file.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Check File If Exists
Read More

Bash Check If File Exists

When working with files in bash, it is essential to know whether the particular file or directory exists.…