How to Recursively Change the File's Permissions in Linux

Updated on

3 min read

Chmod Recursive

If you’re dealing with Linux systems, you will probably encounter a situation when you try to create or edit a file and receive a “Permission deny” error. Typically, errors related to insufficient permissions can be solved by setting the correct file permissions or ownership .

Linux is a multi-user system, and access to the files is controlled through the file permissions, attributes, and ownership. This ensures that only authorized users and processes can access files and directories.

For more information about file permissions, see “Umask Command in Linux” and “Understanding Linux File Permissions “ .

In this article, we’ll explain how to recursively change the permissions of files and directories.

Chmod Recursive

The chmod command allows you to change the permissions of files using symbolic or numeric mode.

To recursively operate on all files and directories under a given directory, use the chmod command with the -R, (--recursive) option. The general syntax to recursively change the file’s permissions is as follows:

chmod -R MODE DIRECTORY

For example, to change the permissions of all files and subdirectories under the /var/www/html directory to 755 you would use:

chmod -R 755 /var/www/html

The mode can also be specified using the symbolic method:

chmod -R u=rwx,go=rx /var/www/html

Only the root, the file owner, or users with sudo privileges can change the permissions of a file. Be extra careful when recursively changing the files’ permissions.

Using the find Command

In most cases, the files and directories should not have the same permissions. Generally the files do not require the execute permission, whereas you must set execute permissions on the directories to be able to change into them.

The most common scenario is to recursively change the website file’s permissions to 644 and directory’s permissions to 755.

Using the numeric method:

find /var/www/html -type d -exec chmod 755 {} \;find /var/www/html -type f -exec chmod 644 {} \;

Using the symbolic method:

find /var/www/html -type d -exec chmod u=rwx,go=rx {} \;find /var/www/html -type f -exec chmod u=rw,go=r {} \;

The find command searches for files or directories under /var/www/html and passes each found file or directory to the chmod command to set the permissions.

When using find with the -exec option, the chmod command is run for each found entry. Use the xargs command to speed up the operation by passing multiple entries at once:

find /var/www/html -type d -print0 | xargs -0 chmod 755 find /var/www/html -type f -print0 | xargs -0 chmod 644

View File Permission

To view the file permissions, use the ls command:

ls -l file_name
-rw-r--r-- 12 leia users 12.0K Dec  11 10:10 file_name
|[-][-][-]-  [---] [---]
| |  |  | |    |     |
| |  |  | |    |     +---------------> 7. Group
| |  |  | |    +---------------------> 6. Owner
| |  |  | +--------------------------> 5. Alternate Access Method
| |  |  +----------------------------> 4. Others Permissions
| |  +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type

In the example above, (rw-r--r--) means that the file owner has read and write permissions (rw-), and the group and others have only read permissions (r--).

Conclusion

The chmod command with the -R options allows you to change the file’s permissions recursively.

To recursively set permissions of files based on their type, use chmod in combination with the find command.

If you have any questions or feedback, feel free to comment.