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
See note from commenter, below, about effect on files ↓

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

, , , , , , , , , ,

8 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!

  4. assembler January 18, 2015 at 4:53 am #

    Great cheat sheet for everyone when they are tired.

  5. Debashish August 19, 2015 at 5:10 am #

    What is “\;” in your commands?

    • Chris Gilligan August 19, 2015 at 7:31 am #

      Debashish,
      For each result of find, chmod {} is executed. All occurences of {} are replaced by the individual filenames found. ; is prefixed with a backslash to prevent the shell from interpreting it. ; moves to the next command. It is often used in a series of commands in scripts… see PHP or javascript. ; is also often used in a simple command line script to perform a number of commands in sequence.

      See http://linux.die.net/man/1/find and look for -exec command ;

  6. Tylla May 4, 2017 at 6:05 am #

    I would like to point out that +X not only sets the execute bit on directories only, it sets on all files as well that have any other execute bit set.

    So if you have something like this:

    drwxr—– somedir
    -rwxr—– somefile

    you’ll end up with:
    drwx–x–x somedir
    -rwx–x–x somefile

    instead of the wanted:
    drwx–x–x somedir
    -rwx—— somefile

    It depends if this is desirable or not, but you should point this out in your post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.