Linux - How to recursively chmod a folder?

Yoodley is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Because Linux is a multi-user operating system, it includes a mechanism for setting and managing file permissions to ensure that only authorized processes and users have access to certain directories and files.

As you use Linux, you may run across situations where you are unable to alter files or directories due to a “Permission denied” error, indicating that you lack the necessary privileges.

This guide will show you how to recursively adjust file permissions in Linux so that your settings are applied to subfolders and files.

How to check file permission(s) in Linux?

When the “permission denied” issue occurs during a file or directory operation, first check the user, group, and other permissions.

You may accomplish this with the ls -l command. The permissions for various files and directories in the “/var” directory, for example, are as follows.

The information in the output above is specific.

The file and directory permissions are represented in the first column. The kind is indicated by the first letter in this column: d denotes a directory, l denotes a symbolic link, and – denotes a regular file.

The remaining nine characters are divided into three groups: u user, g group, and o owner, which indicate file or directory permissions. The letter r denotes “read” rights, the letter w denotes “write” permissions, and the letter X denotes “execute” permissions.

You may easily adjust the file permissions of your working directory or file once you know what they are.

How to recursively chmod a folder in Linux?

In Linux, the chmod command is the simplest way to modify file permissions. The command’s overall syntax is as follows:

chmod {permission}


Permission: The permissions granted to a user, group, owner, or all.

Operator: This indicates the permissions granted to the permission’s assignee: + grants permission, – rejects permission, and = specifies which permissions should be set.

Mode: Mode determines which rights to assign: r read, w write, or x execute.

For example, to prevent everyone from writing to the backup directory in the “/var” directory, use the command:

sudo chmod -w /var/backups

Although the chmod command is useful for modifying file permissions, it only affects the given directory or file.

Fortunately, you can alter the file permissions of a directory or file and its subdirectories and files recursively. Use the chmod command with the recursive -r option to accomplish this.

For example, let’s remove read rights from the “/var/backup” directory and all of its files and subdirectories recursively.

Let us now use chmod to modify the permissions recursively:

sudo chmod –r -r /var/backups

The file permissions have now changed, as you can see.

Recursive change permission with find

Typically, you will not be assigning equivalent rights to files and folders. This is due to the fact that you require the execute permission in order to cd into a specified directory. Most files, however, do not require this execute permission.

To get around this, use Linux’s search utility. This allows you to just search and set permissions on specified files or directories.

Let’s look at the “/var/log” directory. There are both files and folders in the directory.

We can use the search command with the -type f argument to specify particular permissions for files alone.

Here’s an example command:

sudo find /var/log/ -type f -exec chmod 777 {} ;

In the preceding example, we used the find command to look for files in the “/var/log” directory, then set read, write, and execute rights for users, groups, and all.

We can use the same logic for directories. In this situation, the -type d specifies that we only want directories.

As an example:

sudo find /var/log/ -type d -exec chmod 755 {} ;

In the preceding example, we only assigned the permission 755 to directories, rather than both files and directories.

You may check this with the ls -la command.

All files have the permission 777, whereas directories have the permission 755.


Read More

  1. How to Enable Execution of PowerShell Scripts?
  2. How to View and Examine Android Logs?


Please enter your comment!
Please enter your name here