×
chmod recursive

Setting up correct permissions is an essential part of any Linux system administrator in the production environment.

Wrong permissions may harm your server and allow hackers to upload and execute malicious code to your server. Proper permission and ownership is the first defense to secure your server.

Its Highly recommended that you restrict other users and group from accessing and executing files on your server, unless otherwise needed.

In this tutorial, we will show you how to change file permissions recursively with chmod and find command in Linux.

Basic Syntax of CHMOD Recursive

The basic syntax of the chmod command is shown below:

chmod -R rwxrwxrwx path-of-the-directory

Where :

  • -R : This option change files and directories permissions recursively.
  • rwx : First rwx refers to the user permissions. These permissions apply only on the owner of the file and directory.
  • rwx : Second rwx refers to the group permissions. These permissions apply on the group owner of the file and directory.
  • rwx : Third rwx refers to the other permissions. These permissions apply to all other users on the system.

For example, set the permissions on the /var/www/html directory recursively so that owner has full permissions and everyone else has only read and execute permissions:

chmod -R 700 /var/www/html

Or

chmod -R u=rwx,g=---,o=--- /var/www/html

You can see all the options available with chmod command using the following command:

chmod --help

You should see the following screen:

chmod command Examples

In this section we will show you how to change permissions on directory and sub-directories with examples.

If you want to change the permissions of only files located inside specific directory then you will need to apply conditional file permissions recursively.

In the following example, we will find all the files with permission 700 located inside the directory /var/www/html and change them with permission 755.

First, list all the files with permission 700 located inside the directory /var/www/html with the following command:

find /var/www/html -type f -perm 700 -print

You should see the following output:

/var/www/html/index.html
/var/www/html/page2.php
/var/www/html/web1.webdock.io/index.html
/var/www/html/wordpress-4.0.tar.gz
/var/www/html/test/file.txt
/var/www/html/page1.php
/var/www/html/info.php
/var/www/html/page1.html

Next, you can apply new permission 755 using the -exec option as shown below:

find /var/www/html -type f -perm 700 -print -exec chmod 755 {} \;

You should see the following output:

/var/www/html/index.html
/var/www/html/page2.php
/var/www/html/web1.webdock.io/index.html
/var/www/html/wordpress-4.0.tar.gz
/var/www/html/test/file.txt
/var/www/html/page1.php
/var/www/html/info.php
/var/www/html/page1.html

You can now verify new permissions using the following command:

ls -lR /var/www/html/

You should see the following output:

/var/www/html/:
total 5940
-rwxr-xr-x 1 root root 156 Nov 14 16:46 index.html
-rwxr-xr-x 1 root root 22 Oct 6 11:29 info.php
-rwxr-xr-x 1 root root 374 Sep 11 15:32 page1.html
-rwxr-xr-x 1 www-data www-data 84 Sep 11 16:31 page1.php
-rwxr-xr-x 1 www-data www-data 54 Sep 11 15:03 page2.php
drwxr-xr-x 2 root root 4096 Dec 20 16:57 test
drwx------ 2 www-data www-data 4096 Aug 21 12:13 web1.webdock.io
-rwxr-xr-x 1 root root 6051082 Sep 4 2014 wordpress-4.0.tar.gz
/var/www/html/test:
total 0
-rwxr-xr-x 1 root root 0 Dec 20 16:57 file.txt
/var/www/html/web1.webdock.io:
total 4
-rwxr-xr-x 1 www-data www-data 110 Aug 21 12:36 index.html

If you want to find all the directories and sub-directories with permission 644 located inside /var/www/html/directory and change them with permission 766, you can use option -d to find and apply permission only on directories and sub-directories.

find /var/www/html/directory -type d -perm 644 -print -exec chmod 766 {} \;

You should see the following output:

/var/www/html/directory
/var/www/html/directory/dir1
/var/www/html/directory/dir1/dir2
/var/www/html/directory/dir1/dir2/dir3
/var/www/html/directory/dir4
/var/www/html/directory/dir4/dir5
/var/www/html/directory/dir6
/var/www/html/directory/dir6/dir7
/var/www/html/directory/dir8
/var/www/html/directory/dir8/dir9

Conclusion

Thats it for now. You can now easily apply conditional permissions recursively on your desired files and directories. For more information, you can visit chmod manual page at chmod doc.