chmod
can do it, you don't need find.
Use symbolic mode and capital X
.
chmod -R u=rwX,og=rX directory
alternately to avoid repetition, and make easier to edit. We can made it action orientated, instead of role orientated.
chmod -R a=rX,u+w directory
The capital X tells chmod to apply x to directories, (and if it already has it, if you do for example go+X
).
Manual extract:
The format of a symbolic mode is [ugoa...][[+-=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo. Multiple symbolic modes can be given, separated by commas.
A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if a were given, but bits that are set in the umask are not affected.
The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.
The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X), set user or group ID on execution (s), restricted deletion flag or sticky bit (t). Instead of one or more of these letters, you can specify exactly one of the letters ugo: the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file's group (g), and the permissions granted to users that are in neither of the two preceding categories (o).
You're missing a space after 644.
Also, 644 is probably not what you want on a directory. You probably want 755.
Edit to include answer from comments below:
For directories:
find . -type d -exec chmod 755 {} \;
For files:
find . -type f -exec chmod 644 {} \;
There's very likely other ways (maybe shorter) to do this, but this will work.
Best Answer
One way to do exactly what you want with one
find
command is-o
means OR. This command processes all objects in the project tree that are of type “file” or type “directory”, and executes different commands based on which type each object is.You can make this slightly more efficient by replacing the semicolons (
';'
) with plus signs (+
); this tellsfind
to runchmod 644
once, with all the plain files’ names as arguments, and to runchmod 755
once, with all the directories’ names as arguments.By the way, you don’t need to say
./
for anything other than running a program in the current directory (although it does occasionally come in handy for manipulating files whose names begin with some special characters).I wonder what your permissions look like now (before running a recursive tree-wide
chmod
). Files generally don’t get their execute permission bits turned on unless they actually executable programs, or you’ve accidentally done something likechmod -R 755
. Directories generally don’t get their execute permission bits turned off unless you’ve done something catastrophic likechmod -R 644
. If you have badly messed up permissions because you have done an ill-advisedchmod -R
with a numeric mode, ignore this section and stick with the command at the beginning of this answer.But, if your permissions are generally sane, and you just want to impose a standard of read/write permissions for the user, and read-only permissions for everybody else (group and world), you can do
in which
+rX
means+r,+X
(note that theX
is capitalized — this is important).+r
means set read permission for user, group and other.+X
means set execute permission for user, group and other if it is currently set for any of them or if the object is a directory.u+w
means set write permission for user.go-w
means turn off write permission for group and other.Summary of some key points:
chmod
only things that you own (unless you’re privileged) and that are not read-only. If there are things in your directory that you cannotchmod
, any of these commands will cause an error.find
commands will not do anything to things that are not directories or files (e.g., named pipes).find
command with-exec chmod 755 {} +
may fail if you do not already have read and execute permission to all folders under your top level directory.chmod -R +rX,u+w,go-w
command willx
bit; otherwise,