Unix – Recursively chmod only directories or files
Have you ever come across the problem of needing to chmod many directories and sub-directories, but you donโt want to touch any of the files? Maybe itโs the exact opposite, or you need to recursively change the permissions on only files with a specific extension. If so, this article is for you
To change permission of all directories to 755, execute this command in terminal
find . -type d -exec chmod 755 {} \;
This will โfindโ all directories, starting at โ.โ, and chmod them toย 755.
The following snippet does the opposite and finds only files. The difference (beside the file permissions) is the โ-type fโ, โfโ specifies files and โdโ directories.
find . -type f -exec chmod 644 {} \;
Here are some other commands that you can build base on previous
find . -type f -name '*.htm*' -exec chmod 644 {} \;
This lets you โfindโ files (โ-type fโ) with a specific extension (โ-name ‘*.htm*’โ) and chmod them with your desired permission (โ-exec chmod 644 {}โ).
chmod -R o+rX
This snippet will recursively chmod โotherโ (โoโ) permissions with read/eXecutable (โ+rXโ), and the capital X ensures that directories and files set (user/group permissions) to executable will be properly modified, while ignoring regular (non-executable) files.