Recursive chmod: rw for files, rwx for directories

I want to chmod a lot of files and directories. As x indicates list for directories and execute for regular files I'd like to apply rw for files and rwx for directories. Is it possible using only the chmod command?

If it isn't, what is the most convenient way?

Doing a chmod -R 770 isn't a possibility as I don't want the regular files to become executable.


Solution 1:

I do this on occasion using a single find command. Ugly but effective.

find /p/a/t/h \( -type d -exec chmod 755 {} \; \) -o \( -type f -exec chmod 644 {} \; \)

Solution 2:

Use the X permission, from the man page:

The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode. Operations with the perm symbol "X" are only meaningful in conjunction with the op symbol "+", and are ignored in all other cases.

So do the following:

chmod -R a-x [directory]
chmod -R a+rwX [directory]

This removes the execute bit from all files and directories followed by adding read and write privileges to everything, and execute privileges to only directories. (No regular files have the execute bit on anymore from the first step.)

Solution 3:

You can also use find along with xargs:

find . -type f -print0 | xargs -0 chmod 666
find . -type d -print0 | xargs -0 chmod 777

where -type specifies directory or file.