Oct 21, 2023 3 min read

How to chown Recursively on Linux?

Chown recursively on Linux with our step-by-step tutorial. The chown command allows you to change the ownership of files and directories.

chown Recursively on Linux
Table of Contents

Introduction

Before we begin talking about how to chown recursively on Linux, let's briefly understand-What is chown ?

In Linux, the chown command allows you to alter the ownership of files and directories. When dealing with directories, sometimes you need to change ownership recursively, affecting all files and subdirectories within.

This tutorial explains how to use the chown command recursively on Linux. We will also address a few FAQs on how to chown recursively on Linux.

How to chown Recursively on Linux?

To modify the ownership of files or subdirectories in the current working directory, use the "-R" flag with the "chown" command. The chown command's syntax for recursive operations is as follows:

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

The terms "user" and "group" in this context refer to the new user and group names that will take the place of the existing ones.

Example 1: Change the User’s Ownership Only

The following files and folders can be seen in the example's "Sample" directory, which has the username and group "vegastack":

ls -l Sample

Execute the chown command recursively with the "-R" flag as follows to change its ownership:

sudo chown -R anna Sample

The output verifies that "anna" has taken over ownership of the "Sample" directory from "vegastack".

Example 2: Change the Group Recursively

Use the chown command with the "-R" flag and the new group name (e.g., anna:johnson) to recursively update the directory's group:

sudo chown -R anna:johnson Sample

Use the "ls -l" command on the directory as shown below to confirm:

ls -l Sample

The "johnson" group is now linked to the "Sample" directory and all of its contents.

Example 3: Change the Owner and Group Recursively

When using the "chown recursively" command to modify both the owner and the group at once, specify both names in one command as follows:

In this instance, the username and group name for the "johnson" directory's files and subdirectories is "Extra":

ls -l Extra

Using the following command, you can recursively modify the "Extra" directory owner and group name:

sudo chown -R johnson:milton Extra

The "Extra" directory has been recursively allocated the username "johnson" and group name "milton".

Example 4: Find Files and Change Their Ownership

The "chown recursively" command combined with the "find" function allows the user to alter the ownership of numerous files in addition to directories. Use this command to achieve your goal:

sudo find . -type f -name "*.txt" -exec chown anna:johnson {} \;

The command has the following information.

  • find: This component represents the "find" command-line utility.
  • type f: Displays the "type" command, which is useful for determining the type of command. The "f" in this syntax finds files.
  • name: Shows the ".txt" file extension.
  • exec: carry out the chown command.

The command has been successfully carried out. Use the "find" command once more in the following manner to confirm the outcomes:

find ~/ -group johnson -user anna

The output shown above confirms that "anna" is the owner name and the group name “johnson” for each ".txt" file.

FAQs to chown Recursively on Linux

What does it mean to change ownership recursively?

Changing ownership recursively means applying ownership changes to all files and directories within a specified directory, including subdirectories and their contents.

How do I change ownership recursively using chown? 

To change ownership recursively, you can use the -R or --recursive option with the chown command. For example, chown -R username:groupname directory will recursively change the ownership of the specified directory and its contents to the specified user and group.

Can I change ownership recursively without specifying the group? 

Yes, you can omit the group name in the chown command, and it will leave the group ownership unchanged. For example, chown -R username directory will change the ownership of directory and its contents to the specified user, preserving the group ownership.

Do I need root/sudo privilege to use chown recursively? 

Yes, changing ownership of files and directories owned by other users typically requires root or sudo privileges.

What happens if some files or directories are not accessible? 

If chown encounters files or directories that are not accessible due to lack of permissions, it will continue with the remaining items, and you may see error messages for the inaccessible ones.

How can I change ownership recursively of all files and directories within the current directory? 

You can use chown -R username:groupname . to change ownership recursively of all files and directories within the current directory.

Can I simulate ownership changes without actually applying them? 

Yes, you can use the --verbose or -v option with chown to simulate ownership changes and see the list of files and directories that would be affected without actually modifying ownership.

Conclusion

The "chown recursively" command on Linux modifies the group name and ownership of a directory and all of its subdirectories and files. For this action, the "-R (recursively)" flag is used. With the "find" command, it also enables users to recursively alter the ownership of all files. Every possible element of chown on Linux has been covered in this tutorial.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.