Modifying only directory permissions using find

I recently went through my web directories and updated the permissions to be more inline with recommended security practices. When setting web file permissions in Linux you want to set execute permissions on a directory to allow listing and traversal, but you don't want to set execute permissions on the files for security reasons.

Doing this directory by directory by hand would have been a pain so I decided to get creative. Well as far as script-fu goes this is pretty tame(lame?) but it worked well for me.

To set the permissions on a the /var/www/ folder and all sub-folders of use this command:

find /var/www -type d -print0 | xargs -0 chmod -v 755

Conversely if you only want to update the permissions on only files you can use the same command but substitute -type f for -type d.

The -print0 argument will make find null terminate each of the paths instead of newline terminating them and the -0 argument tells xargs to expect argument terminated by null instead of whitespace. Without these options xargs would misinterpret pathnames with spaces as multiple arguments.