Recursive chmod: rw for files, rwx for directories
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.
Related videos on Youtube
Deleted
Updated on September 17, 2022Comments
-
Deleted over 1 year
I want to
chmod
a lot of files and directories. Asx
indicates list for directories and execute for regular files I'd like to applyrw
for files andrwx
for directories. Is it possible using only thechmod
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.-
Admin over 14 yearsDo you really want public write permission on files and directories? That means you don't care who clobbers any of the files?
-
Admin over 14 yearsTrue. I changed it above.
-
-
quack quixote over 14 yearsthis won't force mode 666 on regular files. it doesn't clear the executable bit for regular files if they already have it set.
-
jwaddell over 14 yearsSo in that case you could do
chmod -R a-x [directory]
followed bychmod -R a+rwX [directory]
? -
Ben S over 14 years@jwaddel: That should work. You're removing all execute bits, regardless of file/directory type, then adding execute to only directories and regular files that already have the execute bit on (which is now none.)
-
Deleted over 14 yearsDoesn't xargs have a length limitation? It's a LOT of files and directories.
-
Arjan over 14 yearsDoesn't "{}" need to be quoted, just in case paths hold spaces? Like
find . -type d -exec chmod -vc 755 "{}" \;
(including-vc
for some feedback). (And maybe, just to clarify, add the two standalone commands to the answer as well?) -
Dustin Holtz over 14 yearsNo, {} doesn't need to be quoted (I just tested to be absolutely certain). When find inserts {} into the command, it's properly escaped. I haven't looked up the inner workings of find, but I would assume that it creates an argument array to pass to exec(), and it would strcpy() the filename into the array. When chmod looks at its arguments, it would appear properly.
-
avakar about 14 yearsOnce I do
a-x
, the directory is not accessible. In fact, chmod fails with permission denied and won't remove executable bit from the files within the directory. Is there a way to do this without being root?