Linux
Published in Linux
avatar
3 minutes read

Changing Permissions for a Folder and Its Subfolders/Files

To change permissions for a folder and all its subfolders and files in Linux, you can use the chmod command along with the find command.

Using find and chmod Command

The find command is used to locate files and directories based on various criteria, and the chmod command is used to change file permissions.

Step 1: Navigate to the Target Folder

Open your terminal and navigate to the directory whose permissions you want to modify. You can use the cd command to change directories:

cd /path/to/your/folder

Step 2: Change Permissions for the Folder

To change the permissions for the main folder only, use the chmod command directly on the folder. Replace PERMISSIONS with the desired permission settings (e.g., chmod 755):

chmod PERMISSIONS .

Step 3: Change Permissions Recursively

To change permissions for the folder, its subfolders, and all files within, use the find command in combination with chmod. The syntax is as follows:

find . -type d -exec chmod PERMISSIONS {} \;
find . -type f -exec chmod PERMISSIONS {} \;

Replace PERMISSIONS with the desired permission settings (e.g., chmod 755). The above two commands will modify permissions for directories (-type d) and files (-type f) respectively.

The option -exec is used to execute the chmod command for each found item, and {} represents the placeholder for each item found by find.

Example: Giving Read, Write, and Execute Permissions to the Folder and Its Contents

To give read, write, and execute permissions to the folder and its subfolders and files, you can use the following commands:

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

This will set the permissions to 755 for directories and 644 for files, which are common permission settings for many directories and files.

0 Comment