How to Recursively Change the File’s Permissions in Linux?

In Linux, file permissions are used to assign permissions to different users who can read the files only, and others can write them. File permissions can be changed at any point using some simple command in Linux. File permissions are important to create a secure working environment in Linux.

We can change the file permissions recursively, which allows us to change the file/directory and its sub-directory’s permissions with a single command.

In this article, different ways of modifying the file permissions recursively are discussed. This guide explains the following things:

Before starting the main topic, let’s first discuss the different file permissions available and what are the key differences between them in the next section.

Type of Permissions in Linux

How to install Kali Linux in Window...

There are three types of permissions in Linux, read (r), write (w), and execute (x) permissions which can be assigned to different users. Let’s discuss it briefly:

  • Read (r): The user can get or remove access to read the file.
  • Write (w): The user can get or remove the access to write the file content.
  • Execute (x): The user can get or remove the access to execute the file or see the directory content.

To check the permissions of a file or directory, the “ls -l” is used as discussed below:

$ ls -l

If it is a directory, the first ‘-’ displays the ‘d’; otherwise, it remains empty.

d: The starting “d” of every line represents a directory.

– : The dash () sign at the start of the line shows it’s a file.

User Permissions: The first 3 characters after (d or –) indicate the permissions for the User.

Group Permissions: The next 3 characters represent group permissions.

User Permissions: The next 3 characters of the group shows the permissions for the User.

In the next section, we will show you how the different methods that you can implement to change file permission in Linux recursively.

How to Recursively Change File’s Permissions Using chmod Command?

The chmod command is used to change the privileges of the files recursively. This section will explain different methods to use the chmod command to change file permission. Let’s check the syntax first.

The general syntax of the chmod command to change the file permissions recursively is as follows:

$ chmod -R <permission><operator> <mode>

The chmod is called change mode, and “R” represents the recursive behavior (check again and again) of the command. Permission can be for the user, owner, or owner group, and the operator is replaced with the (+, -, =).

  • The plus (+) sign will be used to grant that permission,
  • The negative () sign will be used to deny the permission
  • The Equal (=) sign will refer to the specific permission.

The mode can be Write(w), Read (r), or Execute.

Example 1: Recursively Change the Permissions for a Single File

We can change the permissions for a single file recursively using the “R” option of the chmod command. Let’s perform different examples to understand this behavior:

Recursively Change Permissions for All Users

We can change a file’s read, write, and execute permissions using the change mode (chmod) command. To change the write permissions of file “testfile2.txt”, we can use the below command:

$ chmod -R -w testfile2.txt $ ls -l testfile2.txt

The output shows the user, owner group, and the owner have no write access to the testfile2.txt.

Similarly, to recursively (R) remove the read (r) and execute (x) permissions for all the users, we will use the following command:

$ chmod -R -r testfile2.txt                # to change read permissions
$ chmod -R -x testfile2.txt                # to change the execute permissions   
  • R – Shows the recursive behavior of the change mode command.
  • -r – It represents the read privileges.
  • -x – The execute permission is used by the “x” option.

Change Permissions for Specific Users

In Linux, we can set separate permissions for every user. The user can have single permission like read (“r”) or multiple permissions, including read, write and execute. If you want to change the permissions of a user, the below ways can be used:

Change Permission for User

The “chmod” command changes the user’s permission. For changing the permissions recursively “R” option is used. To remove the read access of the “testfile2.txt” file for the user, use the below command:

$ chmod -R u-r testfile2.txt
$ ls -l testfile2.txt

Change Permission for Group Owner

If you want to permit a user, add the user to a group, and the group permission will be automatically accessed by the user. After removing the write privileges of “testfile2.txt” for the owner group, the members of the group will not have the write permissions as well. To remove the write privileges recursively for the group, utilize the following command:

Note: The “ls -l” command provides a list with the detail of that directory.

$ chmod -R g-w testfile2.txt
$ ls -l

The commands for removing the execute and read permissions for the group is mentioned below:

$ chmod -R g-x testfile2.txt   #Command for removing execute permissions for group
$ chmod -R g-r testfile2.txt   #Command for removing read permissions for group

Change Permission for Owner

In windows, the owner enjoys all the privileges, while in Linux, the sudo user enjoys all the permissions. We can change the owner permissions (o); for instance: to remove the execute access recursively from the owner for testfile3.txt, run the below-mentioned command:

$ chmod -R o-x testfile3.txt
$ ls -l testfile3.txt

The below commands will remove the “write”, and “read” permissions for the owner of files:

$ chmod -R o-w testfile3.txt   #for removing the write permissions
$ chmod -R o-r testfile3.txt   #foe removing the read permissions

Example 2: Recursively Change the Permission for Multiple Files

We can change multiple files permissions in a single command. The below command will remove the read-privileges from testfile1.txt and testfile2.txt files:

$ chmod -R -r testfile1.txt testfile2.txt $ ls -l testfile1.txt testfile2.txt

Change the Permission for a Specific Directory

Similar to the file, a directory can have different permissions for the users and can be changed with chmod recursive command. To remove the write privileges for testfolder4 recursively, run the below command:

Note: ls -l command is used to get a detailed list of the directory.   

$ sudo chmod -R -w ~/testfolder4

The write permissions are removed, as seen in the output.

Bonus Tip: Change Permission With find Command

The other way to change permission for a file or directory is by using the find command. The find command used for changing the privileges for all the users as written below:

$ sudo find [file/directory] -type [d/f] -exec chmod [privilege] {} \;
  • file – The file will be used to change file permission and select the “f” for type.
  • privilege – It shows the read, write and execute permissions. The absolute number is used for privileges. For instance: 707 means the first 7 is user permissions, 0 is group permissions, and the last 7 is owner permissions.
  • 7 shows allow all permissions, and 0 means remove all permissions.

To give all permissions (7) to the User and Owner and no permission (0) to the group, use the following command:

$ sudo find testfile5.txt -type f -exec chmod 707 {} \;

That’s the end of the guide!

Conclusion

To recursively change the file’s permissions (read, write or execute) in Linux, the chmod and find commands are used. The commands to recursively change a file permissions are “chmod -R <permission><operator> <mode>” using chmod, and “sudo find <file/directory> -type <f/d> -exec chmod <757> {} \;” using the find method. This post has elaborated on the possible methods to change the file’s permission in Linux recursively.