Buy @ Amazon

Recursive chmod settings based on file or directory


I recently had to do this and below are the commands that I ran to get things done.
$ find ./my-folder -type d -exec chmod 755 {} \;
$ find ./my-folder -type f -exec chmod 644 {} \;
where the pattern is:
$ find [search-path] -type [d | f] -exec chmod [access-rights] {} \;
Explanation:
-type d => files of type directory
-type f => file of type "file", a non-directory
{} => replaces every file path found at the find command
\; => is to escape the semi-colon. And semi-colon is required to denote the end of command.
 If not escaped, the shell interprets it instead of find command.
755 => make a file readable/executable by everyone and writable by the owner only.
644 => make a file readable by anyone and writable by the owner only.


And as I was doing this, I tried reading a discussion in stack-overflow on this and found an even simpler command:
$ chmod -R a+rX ./my-folder
where the pattern is:
$ chmod -R a+rX [directory-name]
Explanation:
-R => recursively change permission
+ => add permission settings on the given role. Use - to remove permission settings against a role.
a => a implies all roles. Others being u for user, g for group, o for others
r => r implies read permission. Others being w for write permission and x for execute permission.

Note: I used X (capital) and not x (lower case). What does it mean? The capital X implies that if the file to be operated on is a directory then make it executable, else leave its executable permission bit untouched. It is also interesting to learn that this permission symbol makes sense for "+" operations and are ignored in other cases.

Further reading and references: