Recursive chmod: rw for files, rwx for directories

30

11

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.

Deleted

Posted 2009-10-06T18:56:47.223

Reputation: 3 548

1Do you really want public write permission on files and directories? That means you don't care who clobbers any of the files? – Jonathan Leffler – 2009-10-08T04:48:15.137

True. I changed it above. – Deleted – 2009-10-10T09:36:22.177

Answers

19

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 {} \; \)

baumgart

Posted 2009-10-06T18:56:47.223

Reputation: 1 176

Doesn'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?) – Arjan – 2009-10-10T09:46:24.997

No, {} 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. – baumgart – 2009-10-14T14:42:24.917

39

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.)

Ben S

Posted 2009-10-06T18:56:47.223

Reputation: 1 902

Once 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? – avakar – 2010-03-09T09:34:10.757

1this won't force mode 666 on regular files. it doesn't clear the executable bit for regular files if they already have it set. – quack quixote – 2009-10-07T06:23:37.283

So in that case you could do chmod -R a-x [directory] followed by chmod -R a+rwX [directory] ? – jwaddell – 2009-10-08T04:12:03.447

@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.) – Ben S – 2009-10-08T15:20:33.437

14

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.

John T

Posted 2009-10-06T18:56:47.223

Reputation: 149 037

1xargs is smart enough to break long command lines to multiple executions. Consult your xargs manpage and be sure to check xargs --show-limits. – Mike – 2014-07-22T21:40:06.490

1Doesn't xargs have a length limitation? It's a LOT of files and directories. – Deleted – 2009-10-10T09:41:04.943

2I would do directories first, then files. Otherwise, you would risk to miss some subtree due to lack of permission. Also, you should check if the list is scouted before or after the chmod, otherwise, same as above. And yes, I would refrain from using xargs. – Stefano Borini – 2009-10-10T10:20:10.233