How to recursively change the file or directory permissions in Linux?

How to recursively change the file or directory permissions in Linux?

This tutorial will explain you How to Recursively Change the File's Permissions in Linux? You will learn about how to change file or ditectory permissions recursively using the chmod command with -R option. We will learn how conveniently modify the permissions for multiple files at once.

Let's get started.

Changing the file permissions

To change the file permissions in Linux, we use the chmod commad that's stand for “change mode”. It change the permissions of files and directories.  

Command Syntax:

chmod OPTIONS PERMISSIONS FILENAME

You can specify how the permissions should change with OPTIONS argument, the most common option is -R, which stands for “recursive”. This option changes the permissions of all the files and subdirectories within the specified directory. 

Permission Types:

There are three types of permissions can be set: owner, group and other. With the help of PERMISSIONS argument, you can specify the new permissions of the file or directory.

  • Owner = Creator of the file or directory
  • Group = A collection of users who have access the files or directories. 
  • Other = Any user who is not the owner or a member of the group.

Permission Values:

Each type of permission can be set to one of three permission values: read, write, or execute.

  • Read permission (r) = Allows a user to read the file or directory.
  • Write permission (w) = Allows a user to modify the file or create new files in a directory.
  • Execute permission (x) = Allows a user to execute a file or directory.

Frequently using of the chmod command to alter the permissions of a single file, It is essential to know how to change the permissions of multiple files and directories at once. 

Chmod Recursive change the file permissions

We use chmod command in liux to recursively change the file or directory permissions. It changes the permissions of the specified file or directory and all subdirectories and files within it as well. You have to  add -R option to use this feature in linux. 

Syntax

chmod -R PERMISSION DIRECTORY

Read also: Apache log location and how to check log files in ubuntu?

Example:

Let's say you have a directory called “codersvibe” which contains two subdirectories: “dirOne” and “dirTwo”. Each of these subdirectories contains serveral files as well. You want to give read, write and execute permissions to the owner of each file and only read and excecute  permission to every one else. You can use following command to change the permissions. 

chmod -R u+rwx,g+rx,o+rx codersvibe

This command will change the permissions of all subdirectories and files within the codersvibe directory. The owner of the each file have read, write and execute permissions, on the other hand, everyone have only read and execute acess for each file. 

You can use the octal notation as well to get the same results. 

chmod -R 755 codersvibe

Change Ownership of the file or directory

You can use the chown command along witht eh -R option to change the ownership of the file or directory recursively. 

Example:

Let's say you want to change the ownership of the codersvibe directory and it's all files and subdirectories as well to user “masteruser”. You can use the following command to update the ownership:

chown -R masteruser codersvibe

This command will change the ownership of the codersvibe directory and it's all subdirectories and files as well to “masteruser” user.

Conclusion:

We have leared about how to recursively change the file or directory permissions in linux? There are two command chmod and chown along witht the -R to perform the action recursively. 

 

I hope it will help you. 

Happy Learning :)

Leave a Comment

Your email address will not be published. Required fields are marked *

Go To Top
×