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 onlySee 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 {} \;
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.
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?
If
ls -l
orls -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.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!
Great cheat sheet for everyone when they are tired.
What is “\;” in your commands?
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 ;
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.