How to Change Directory Permissions in Linux Recursively

Posted on

As shown in the title of this article, this article is written to describe or to show how to change directory permissions in Linux operating system recursively. In order to do that, an effective way to achieve it is by using an appropriate command executed. The command itself usually executed in a command line interface or a CLI-like utility such as terminal.

In the terminal, there will be a bash-prompt provided or a shell bash-prompt available to execute the command for changing directory permissions in linux recursively. Depends on the user logged-in, usually a normal user can have a default shell-bash prompt for executing command.

So, in order to change the permission of a folder, below is the command pattern :

chmod -Rv permission_attribute directory_target

But the above command execution will change not only directories available inside the directory named ‘directory_target’ recursively but also the files available, stored and located inside the ‘directory_target’ recursively. How to solve the problem for this kind of thing ?. It will be solved by modifying the above command pattern. The command pattern for changing permissions only in the directory recursively and it will not even change the files’s permissions is shown below :

find /directory_target/ -type d -exec chmod permisssion_attribute {} +

For an example it can be shown below taking a laravel-based web application directory. As it can be seen, the directories available inside, it has a permission of ‘755’ or it can be viewed as ‘rwxr-xr-x’ in the line which has an element id of ‘d’ :

user@hostname:/var/www/html/laravel-test# ls -al
total 232
drwxr-xr-x 13 user     user    4096 Oct 29 07:18 .
drwxr-xr-x 19 user     user    4096 Oct 29 07:14 ..
drwxr-xr-x  6 user     user    4096 Aug 30 16:55 app
-rwxr-xr-x  1 user     user    1686 Aug 30 16:55 artisan
drwxr-xr-x  3 user     user    4096 Aug 30 16:55 bootstrap
-rw-r--r--  1 user     user    1380 Aug 30 16:55 composer.json
-rw-r--r--  1 user     user  138834 Oct 29 07:16 composer.lock
drwxr-xr-x  2 user     user    4096 Aug 30 16:55 config
drwxr-xr-x  5 user     user    4096 Aug 30 16:55 database
-rw-r--r--  1 user     user     572 Oct 29 07:16 .env
-rw-r--r--  1 user     user     521 Aug 30 16:55 .env.example
-rw-r--r--  1 user     user     111 Aug 30 16:55 .gitattributes
...

user@hostname:/var/www/html/laravel-test#

The changes can be achieved by performing the above command pattern into the following command pattern :

user@hostname:/var/www/html# find /var/www/html/laravel-test/ -type d -exec chmod 775 {}

This is how the command executed in the shell-bash prompt to achieve the purpose of changing the directory’s permission :

user@hostname:/var/www/html/laravel-test# cd ..
root@soulreaper:/var/www/html# find /var/www/html/laravel-test/ -type d -exec chmod 775 {} +
You have new mail in /var/mail/root
root@soulreaper:/var/www/html# ls -al

After executing the above command pattern, the output or the result can be shown as follows :

user@hostname:/var/www/html/laravel-test# ls -al
total 232
drwxrwxr-x 13 user     user    4096 Oct 29 07:18 .
drwxrwxr-x 19 user     user    4096 Oct 29 07:14 ..
drwxrwxr-x  6 user     user    4096 Aug 30 16:55 app
-rwxr-xr-x  1 user     user    1686 Aug 30 16:55 artisan
drwxrwxr-x  3 user     user    4096 Aug 30 16:55 bootstrap
-rw-r--r--  1 user     user    1380 Aug 30 16:55 composer.json
-rw-r--r--  1 user     user  138834 Oct 29 07:16 composer.lock
drwxrwxr-x  2 user     user    4096 Aug 30 16:55 config
drwxrwxr-x  5 user     user    4096 Aug 30 16:55 database
-rw-r--r--  1 user     user     572 Oct 29 07:16 .env
-rw-r--r--  1 user     user     521 Aug 30 16:55 .env.example
-rw-r--r--  1 user     user     111 Aug 30 16:55 .gitattributes
...

user@hostname:/var/www/html/laravel-test#

Leave a Reply