chmod Recursively – Change Files and Folders Permissions Recursively In Linux

Linux uses file permission in order to regulate access, modification of the files, and folders. This regulation is called files and folder permissions where every file and folder can be set for different permission for different operations. By using the permission unwanted users can not access, read, or change given files and folders. chmod recursive is a popular operation where given access permissions are set for the specified folder and all of its contents and sub-folders and files.

Change Files and Folders Permissions Recursively with chmod

The chmod command can be used with the -R or --recursive options in order to change files and folders permission recursively. The general syntax is like below.

  • MODE is the permission mode which will be set all files, folders, sub-folders, and their contents.
  • DIRECTORY is the directory name or path where the recursive permission change will be applied. DIRECTORY can be a relative path or absolute path.

Lets make an example where we will set permission as 755 for all files and sub-folders under the /var/www .

$ chmod -R 755 /var/www

Only the owner and root user can change permissions and if you are different than that you should log in as root or use the sudo command which provides the root privileges. If you get permission error for the previous command with adding the sudo command like below.

$ sudo chmod -R 755 /var/www

Alternatively, symbolic permissions can be used for recursive permission change. By using the symbolic permissions owner, group, and other users read, write, and execute permissions can be set recursively.

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

Change Files and Folders Permissions Recursively with find

The find command is used to search and find files and folders. But the find command provides different features where one of them is the ability to run commands for the results. The -exec option is used to run chmod command for the search results. We will use very similar chmod command. The "{}" will be used to put search result.

$ find /var/www -exec chmod 755 {} \;

We can also change permissions recursively for only files for the given /var/www path files. We will also provide the -type f option.

$ find /var/www -type f -exec chmod 755 {} \;

$ find /var/www -type f -exec chmod u=rwx,g=rx,o=rx {} \;

This can be also used for only directories to change folder permissions recursively.

$ find /var/www -type d -exec chmod 755 {} \;

$ find /var/www -type d -exec chmod u=rwx,g=rx,o=rx {} \;

Change Specific File Names and Extension Permission Recursively

You may need to change files permission recursively according to their names or permission. The find command can be used for specific file names and extensions. In the following example, we will change only the text files which extension is *.txt as 700.

$ find /home/ismail -type f -name *.txt -exec chmod 700 {} \;

$ find /home/ismail -type f -name *.txt -exec chmod u=rwx {} \;

Leave a Comment