Recursive chmod: rw for files, rwx for directories

33,364

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.

Share:
33,364

Related videos on Youtube

Deleted
Author by

Deleted

Updated on September 17, 2022

Comments

  • Deleted
    Deleted over 1 year

    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.

    • Admin
      Admin over 14 years
      Do you really want public write permission on files and directories? That means you don't care who clobbers any of the files?
    • Admin
      Admin over 14 years
      True. I changed it above.
  • quack quixote
    quack quixote over 14 years
    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.
  • jwaddell
    jwaddell over 14 years
    So in that case you could do chmod -R a-x [directory] followed by chmod -R a+rwX [directory] ?
  • Ben S
    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
    Deleted over 14 years
    Doesn't xargs have a length limitation? It's a LOT of files and directories.
  • Arjan
    Arjan over 14 years
    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?)
  • Dustin Holtz
    Dustin Holtz over 14 years
    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.
  • avakar
    avakar about 14 years
    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?