HomeLinuxRecursively Change Permissions with CHMOD in Linux

Recursively Change Permissions with CHMOD in Linux

In Linux controlling file permissions is essential for maintaining security and appropriate access control. Distinct permissions for files and directories inside a folder are frequently necessary. Sometimes you need the same permission to set for all files/directories inside a folder. Manually doing this file-by-file or all things will be a daunting task.
This tutorial focuses on how to change permissions for all files and directories in a folder, covering both chmod recursive changes and how to apply different permissions to specific types of files or directories.

Changing Permissions for All Files and Directories throughout the Folder

You can change permissions to all files and directories within a folder using the chmod command with the option -R which stands for recursive. You would do this with the -R option. That means it will recursively set permissions for everything in that folder:

chmod -R 755 /your/folder

You can adjust the permission value as needed by replacing 755 with your desired permission level. Make sure to change /your/folder to the path of the folder you want to apply the permissions to.

What Do CHMOD 755, 644, and Others Mean?

Setting Different Permissions for Files and Directories

The find command in Linux can be used to apply different permissions for files and directories. This could be helpful in case you want to avoid execute permission from being set on all the files that don’t need it, such as images, scripts, or text files.

Change Permissions for All Directories to 755:
find /your/folder -type d -exec chmod 755 {} \;

This command will look for all the directories inside /your/folder and change its permission to 755, giving full permission to the owner while giving the capability of reading and executing the contents of the directory to others. (Here “-type d” in the command stands for type directory.)

Change Permissions for All Files to 644:
find /your/folder -type f -exec chmod 644 {} \;

The permission-changing command is only applicable to files, as indicated by the -type f option in the find command, which stands for “type file.”

Changing Permissions for Specific File Types

For some kinds of files (images, CSS, JavaScript, HTML) execution permissions are not needed. Well, you can change permissions for those kinds of files quite easily using chmod:

chmod 644 /your/folder/img/* /your/folder/js/* /your/folder/html/*

By this command, permissions for files inside img, js, and html directories are set to 644, so everyone can read the files, while only the owner can write.

Securing Code Files (e.g., PHP) with More Restrictive Permissions

If you have files that contain sensitive codes, such as PHP scripts, you probably want to lockdown those files so that only the owner can access them. Use the following command:

chmod 600 /your/folder/php/*

This command sets the permissions to 600, and by doing this only the owner can read and write the file, while others cannot access it at all.

Setting Default Permissions for Newly Created Files/Directories

You will want to change the umask value if you want newly created files and directories to have certain permissions. umask defines the default permissions given for newly created files and directories.

Set umask in a PHP Script:

If you create files programmatically using PHP, you can set the umask in your code:

<?php
umask(0022); // Set default permissions
// Your code here
?>
Set umask in the Shell:

To set the umask for your shell session, add the following command to your ~/.bashrc file:

umask 022

This ensures that new files in your shell have the correct default permissions.

Fine-Tuning Permissions

Sometimes you might not want to have a universal permissions change on all your files and directories. How you can refine this using find:

Apply Permissions to Directories Only:
chmod 755 $(find /your/folder -type d)
Apply Permissions to Files Only:
chmod 644 $(find /your/folder -type f)

These commands allow you to apply permissions more selectively, ensuring that you only modify directories or files as required.

Changing Ownership Recursively Using chown

In addition to changing permissions, you can adjust the ownership of files and directories. To do this recursively (throughout the folder), use the chown command as in the following format:

sudo chown -R username:group /your/folder

This command ensures that the specified user and group own all the files and directories within the folder.

Changing permissions for all files and directories in a folder is a common task with chmod command in Linux system administration. By using these Linux commands chmod, chown, find, you can maintain proper permissions on all files and directories in a folder, improving both security and functionality.

Scroll to Top