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.

  • 1
    Do you really want public write permission on files and directories? That means you don't care who clobbers any of the files? – Jonathan Leffler Oct 8 '09 at 4:48
  • True. I changed it above. – Deleted Oct 10 '09 at 9:36

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 {} \; \)
  • 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 Oct 10 '09 at 9:46
  • 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 Oct 14 '09 at 14:42

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

  • 1
    this 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 Oct 7 '09 at 6:23
  • So in that case you could do chmod -R a-x [directory] followed by chmod -R a+rwX [directory] ? – jwaddell Oct 8 '09 at 4:12
  • @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 Oct 8 '09 at 15:20
  • 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 Mar 9 '10 at 9:34

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.

  • 1
    Doesn't xargs have a length limitation? It's a LOT of files and directories. – Deleted Oct 10 '09 at 9:41
  • 2
    I 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 Oct 10 '09 at 10:20
  • 1
    xargs is smart enough to break long command lines to multiple executions. Consult your xargs manpage and be sure to check xargs --show-limits. – Mike Jul 22 '14 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.