Unix – Recursively chmod only directories or files

directories

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.

Leave a Comment