How To Change Folder and Subfolder Permissions In Linux?

Permissions are an important part of Linux security and folders are used with different permissions to protect them. Linux provides read, write and execute permissions by default for files and folders. there are different ways and commands to change folder and subfolder permissions. The defacto command is chmod command which changes the read, write and execute permissions for folders in Linux. Also, the find command can be used to change folder permissions for different cases according to the folder name, folder path, folder owner, etc.

List Folder Permission

Before working with the folder permission in Linux listing and displaying existing folder permissions is very important. The ls command can be used to list current permissions about folders. Also the -d and -l options are provided to list folders with permission information.

$ ls -l -d */
List Folder Permission

Change Single Folder Permission

The chmod command is used to change folder permission. The permission value is specified after the chmod command. In the following example, we set the “Downloads” folder permission as “777” which means everyone can be read, write and execute the “Downloads” folder.

$ chmod 777 Downloads
Change Single Folder Permission

From the output, we can see that the “Downloads” folder permission is set as “777” and colored as “green” to highlight this situation. Also, the first column provides “drwxrwxrwx” which means this is a directory with the specified permissions.

Change Multiple Directories Permissions

The chmod command can be also used to change multiple directories permissions in single command execution. The folders we want to change permission are provided after the permission value. In the following example, we change the permissions for the folders “Downloads“, “Music“, “Pictures“.

$ chmod 777 Downloads Music Pictures

Change Folder and Subfolders Permissions Recursively

By default, the chmod command changes only specified folder permission not the contents of the folders with its subfolder. But the recursive option can be specified to the chmod command in order to change the specified folder and all subfolders permissions with a single command. In the following example, we change permission for “Downloads” and all subfolders.

$ chmod 777 Downloads Music Pictures

The recursive option changes both folders and files permission for the specified folder. But if we do not want to change files permissions and change only the folders located under the specified path the following command can be used. This command finds folders with the find command and changes only folders permissions, not the files.

$ chmod 755 $(find /home/ismail/Downloads -type d)

Change Folder Permission According to the Folder Name

The find command can be used as a helper to the chmod command. By using the find command folders can be searched according to their names and their permissions can be changed properly. Another advantage of the find command is it searches recursively for the specified path for only folders, not files.

$ find /home/ismail/Downloads -type d -exec chmod 711 {} \;

Folder permissions with the specified names can be changed like below.

$ find /home/ismail/Downloads -name "*html*" -type d -exec chmod 711 {} \;
See also  How To Remove (Delete) Symbolic Links In Linux?

Leave a Comment