How to Change File Permissions Recursively in Linux?

SSH commands allows user to manage files and directories easily. We have shared multiple articles on related topics. Today, we’re going to discuss in-detail procedure to change file permissions recursively in Linux.

Change File Permission Recursively with SSH commands

In this tutorial we will discuss the following.

  1. Change File Permissions Recursively from SSH Terminal
  2. Change File Permissions Recursively via PHP file in cPanel

Let us go through the procedure.

#1.  Change File Permissions Recursively from SSH Terminal

We will execute multiple commands to change file permission via SSH terminal. So login to SSH with the required credentials. You may check the video tutorial to login to SSH.

Before proceeding to change file permission, I will suggest you to check the current file permission of the files inside the path or directory for which you want to change the permission. Execute ls -l command and list of all the files with their permission will appear. Now, let us go through the commands to change files permission recursively.

To change the permission of single file we use chmod command as:

 chmod 0666 /home/new/abc

In the above command,

  1. 0666 is the file permission
  2. /home/new/abc is the path of the file of which I want to change the permission. You can change them as per your requirement.

We will now change file permission of multiple files in a current directory or files inside a directory.

  • How to Change Multiple File Permissions?

Use the above command with multiple file names as:

 chmod 0744 /home/new/abc /home/new/test
  • How to Change File Permission Recursively in a directory?

Execute the below given command to do the same.

 chmod -R 0740 path/of/the/directory

To verify the changes use the ls -l  command to list files with their file permissions.

Now, to execute the above command for changing file permission recursively, directory should have executable permission. So, you can use the following command to change file permission recursively in a directory not having executable permission.

chmod -R u=rwX,go=rX /path/of/the/directory
  • How to Change File Permission of the files from one to another?

In this we can change the file permission of the files inside a particular sub-directory or with a particular file permission.

  1. First we will check the files with file permission 0755. Execute the below command to print all the files with 0755 permission.
    find /path/of/the/directory -type f -perm 0755 -print
  2.  Change the files with file permission 0755 to 0644. Execute the below given command to change the file permission of set of files.
    find /path/of/the/directory -type f -perm 0755 -exec chmod 0644 {} \;
  3. If you want to change the file permission of all the files in a directory to 644 then simply use the below given command.
    find folder -type f -exec chmod 0644 {} \;

You can verify the same by using ls -l  command.

  • How to Change File Permissions Recursively excluding certain folder?

With the below given command you can change the file permission recursively excluding certain folder.

find /path/of/directory/ -not -path "/pathof/directory/not/include*" -type f -perm 755 -exec chmod 644 {} \;

This command will exclude the directory which you will mention after -not -path  in the above command.

  • How to Change File Permissions Recursively of the same file name?

With the below given command, you can change file permissions of the same file name.

find /home/your_account/public_html -name filename | xargs chmod 444

Now, if I want to change permission of all the files named test.php  then the above given command would be as given below.

find /home/your_account/public_html -name test.php | xargs chmod 444

If a user does not have shell access, then also you can easily change all the file permissions by running shell commands in cPanel. We will go through the steps to change file permission from cPanel.

#2. Change File Permissions Recursively via PHP file in cPanel

Without having shell access, you can easily change file permissions recursively via PHP file. We have shared an article to run shell commands using PHP file in cPanel and how to change file permission in cPanel as well.

Now, suppose you want to change the file permission of all the files in a directory or sub-directory, then you can simply do that from cPanel without running the PHP file. But what if you want to change the file permission of all the files named test.php from 0644 to 0755 or any other.

So, instead of finding a file with the same name in whole cPanel, you can simply run the shell command using PHP file to change file permission. Let us see the whole procedure to do the same.

  1. Login to your cPanel account and get inside File Manager section.

    Feature List Page
    Select File Manager
  2. Create a php file inside public_html.

    Create New PHP File
  3. Right click over the file and Edit.

    Edit PHP File
  4. Write the below code inside the file.
    <?php 
    $output = shell_exec(' find /home/account/public_html -name .test.php| xargs chmod 644
    '); 
    echo "<pre>$output</pre>"; 
    ?>
  5.  Click over Save Changes to save the code.

    Write the code & Save Changes
  6. Now, hit the file from the browser. You will receive a blank page. Get back to cPanel and reload the page to verify the changes.

That’s It !

Hope the article better describes the procedure to change file permissions recursively. Do share it with your colleagues if you find it working. Also share your suggestions and drop your queries to start the discussion on the related topic. Lets connect on social media with the below links.

Facebook Page: https://facebook.com/redserverhost                                                                                       Twitter Page: https://twitter.com/redserverhost.com

Scroll to Top