Contact Menu

Recursive chmod Tricks

Recursively chmod only directories
find . -type d -exec chmod 755 {} \;

Similarly, recursively set the execute bit on every directory
chmod -R a+X *
The +X flag sets the execute bit on directories only

Recursively chmod only files
find . -type f -exec chmod 644 {} \;

Recursively chmod only PHP files (with extension .php)
find . -type f -name '*.php' -exec chmod 644 {} \;

, , , , , , , , , ,

4 Responses to Recursive chmod Tricks

  1. Jon Jennings July 21, 2011 at 5:22 pm #

    Excellent – thanks. That last one was what I was looking for… spent too long trying to escape filenames containing spaces & hyphens into chmod with something like
    chmod 640 $(find . -name *.php)
    but your way works perfectly.

  2. Giancarlos Colasante August 24, 2012 at 12:03 am #

    Hello, i was trying to chmod a subfolder group and i read in somewhere this command:
    ” find -type d -print0|xargs -0 chmod 644 “, i useded and all my folder disappear, they are there but lost directory atribute, ls -l command return d????????? ? ? ? ? ? _7segment/ and i can’t view content. How can i repair this?

    • Chris Gilligan August 24, 2012 at 3:59 pm #

      If ls -l or ls -al shows d????????? then you probably should try to chmod again to 644 using the method described above, the first chmod command in this post.

  3. Francis September 18, 2012 at 9:45 am #

    So I went trawling the web for an elegant and simple solution to this and decided to write a little script for this myself.

    It basically does the recursive chmod but also provides a bit of flexibility for command line options (sets directory and/or file permissions, or exclude both it automatically resets everything to 755-644). It also checks for a few error scenarios.

    Check it out:
    http://bigfloppydonkeydisk.blogspot.com.au/2012/09/recursively-chmod-only-files-or.html

    Hope it helps!

Add Comment Register



Leave a Reply