Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

chmod
linux

How to apply chmod recursively

Educative Answers Team

While using Linux, you may encounter the error, “permission denied”. This happens when a user does not have the privileges to make edits to a file. This can be resolved by the use of the chmod keyword.

widget

However, if the user needs to gain privileges for more than one file, they can apply chmod recursively by using chmod with the -R flag.

 sudo chmod -R Mode Directory
  • Mode tells the permissions allowed to the user.
  • Directory is the subfolder containing all the files that need to be allowed certain privileges.

Here is an example in which we can change the permissions for /var folder including all its subfolders and files in a single command:

sudo chmod -R u=rwx,go=rx /var

Remember that only root or user with Sudo privileges can change permissions for files and folders.

The user may also want to differentiate the permissions allowed to files and directories. Usually, files have the mode set to 644644 and directories have the mode set to 755755.

Users can use the find method to implement this by using:

sudo find Directory -type t -exec chmod Mode {} \
  • Mode tells the permissions allowed to the user.
  • Directory is the subfolder containing all the files that need to be allowed certain privileges.
  • t is the type: - d for directory and f for file.

The following code allows read and write permissions to files and read, write, and execute permissions to:

sudo find /var -type f -exec chmod 644 {} \
sudo find /var -type d -exec chmod 755 {} \

The mode can also be added using the symbolic method, as used in the example above.

RELATED TAGS

chmod
linux
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring