Using chmod Recursively in Ubuntu

  • Post author:
  • Post comments:0 Comments
  • Reading time:3 mins read
Using chmod Recursively in Ubuntu

Every Linux user has surely come across an error saying “Permission deny”. This error comes when we don’t have sufficient permission on that file or directory where we are trying to do a modification. We can resolve this error by giving sufficient permission using the chmod command or by using chmod recursive (-R) to give permissions recursively.

We can do various tasks using the chmod command. Some of them are listed below.

  1. Changing single file/directories permission.
  2. Changing multiple files/directories permission recursively using chmod recursive (-R).
  3. Changing permissions of only files.
  4. Changing permissions of only directories.
  5. And many more.

In this tutorial, we are going to see how to use the chmod command and how we can change files/directories permission recursively using chmod recursive (-R). Also, some other tasks that we can do with the chmod command.

Basic Syntax of chmod Recursive Command

The basic syntax of changing file/directories permission recursively using chmod is given below.

chmod -R [Mode] [File]

Where :

  • -R :- This option is used to change permissions recursively.
  • Mode :- In this, we need to specify what permission we want to give.
  • File :- In this, we need to give the path of files/directories where we want to give permission.

Let’s See an Example of chmod Recursive Command

In the below example, we will be giving 777 permission (i.e Read, Write, and Execute permission to Owner, Group, and Others) to the test directory and to all the files & directories inside the test directory using a single line command given below.

$ chmod -R 777 test/

In the below image, we can see that the test directory and files & directories inside the test directory are currently not having 777 permission.

Checking exsisting permission of files/directories.

Now let’s execute the above command.

Giving permissions recursively using chmod recursive (-R).

As we can see in the above image after executing the chmod recursive command 777 permission was given to test directory and files & directories inside the test directory. In this way, we use the chmod command recursively.

Few More Examples of chmod Command

Giving Permissions Only to Directories

In the below example, we will be giving 777 permission only to directories inside the test directory using the single line command given below.

$ find test/ -type d -exec chmod 777 {} \;

After executing the above command we can see that all the directories inside the test directory are now having 777 permission.

chmod recursive directories.

Giving Permissions Only to Files

In the below example, we will be giving 777 permission only to files inside the test directory using the single line command given below.

$ find test/ -type f -exec chmod 777 {} \;

After executing the above command we can see that all the files inside the test directory are now having 777 permission.

chmod recursive files.

Conclusion

In this tutorial, we learned how to use the chmod command also how to give permissions recursively using the chmod recursive (-R) option in Ubuntu.

Leave a Reply