Change all file permissions to 644 and all folder permissions to 755 recursively

chmoddirectorypermissionsrecursive

How to change all file permissions to 644 and all folder permissions to 755 recursively using chmod in the following two situation:

  • If they had 777 permissions
  • Regardless of the permission (with ANY permissions)

Best Answer

find . -type d -perm 777 -exec chmod 755 {} \; (for changing the directory permission)

find . -type f -perm 777 -exec chmod 644 {} \; (for changing the file permission)

If the files/directories dont have 777 permissions, we easily remove the -perm 777 part. The advantage of these commands is that they can target regular files or directories and only apply the chmod to the entries matching a specific permission.

. is the directory to start searching

-type d is to match directories (-type f to match regular files)

-perm 777 to match files with 777 permissions (allowed for read, write and exec for user, group and everyone)

-exec chmod 755 {} \; for each matching file execute the command chmod 755 {} where {} will be replaced by the path of the file. The ; indicates the end of the command, parameters after this ; are treated as find parameters. We have to escape it with \ since ; is the default shell delimiter, it would mean the end of the find command otherwise.