How do I change permissions for a folder and its subfolders/files?

 

To change permissions for a folder and its subfolders and files recursively in Linux, you can use the chmod command along with the find command. Here's how you can do it with an example:

Syntax:

bash
find /path/to/folder -type d -exec chmod permissions {} \; find /path/to/folder -type f -exec chmod permissions {} \;
  • /path/to/folder: Replace this with the path to the folder for which you want to change permissions.
  • permissions: Replace this with the desired permissions in numeric or symbolic notation (e.g., 755 or u+rwx,go=rx).

Example: Let's say you want to change the permissions of a folder named /myfolder and all its subfolders and files to 755, which provides read, write, and execute permissions for the owner and read and execute permissions for others.

bash
find /myfolder -type d -exec chmod 755 {} \; find /myfolder -type f -exec chmod 644 {} \;

In this example:

  • The first find command looks for directories (-type d) under /myfolder and applies the chmod 755 command to each of them. This sets the permissions to 755 for directories, which allows the owner to read, write, and execute and others to read and execute.

  • The second find command looks for files (-type f) under /myfolder and applies the chmod 644 command to each of them. This sets the permissions to 644 for files, which allows the owner to read and others to read but not write or execute.

By using these two find commands in combination with chmod, you can change the permissions of both folders and files within the specified directory and its subdirectories recursively.

Please be cautious when changing permissions, especially when giving write or execute permissions, as it can affect the security and functionality of your files and directories. Make sure to understand the implications of the permissions you are setting.

Comments