Filesystems – Recursively Change File Permissions but Not Directories

chmodfilesystemsfindpermissions

I was doing a mass recursive change of permissions of some files that I had migrated to a unix system. I changed them to ug+rw, but then I found that I could not traverse subdirectories. I looked at the man page for chmod and didn't see any explanation for excluding directories, so I googled a little and found that people used find to recursively change the permissions on directories to 'execute' for user and group. I did that and then I could look into them.

But it seemed to me that I should be able to be able to do this find chmod — to recursively change the files to read/write but not make the directories untraversable.
Have I done this the 'right' way or is there a simpler way to do it?

Best Answer

The better solution should be

chmod -R ug=rwX,o=rX /path

where the capital X means: set execute bit if

the file is a directory or already has execute permission for some user

(quoted from chmod man page).

Or also, if you want to use find

find /path \( -type f -exec chmod ug=rw,o=r   {} + \) -o \
           \( -type d -exec chmod ug=rwx,o=rx {} + \)