I've been trying to clean up permissions on a few boxes and have been scouring the chmod man as well as all the internet documentation that I an handle without any luck -- so here we go.

Basically, I've got a directory with many sub directories and files -- and I'd like to set the following permissions:

For directories: 770 (u+rwx, g+rwx, o-rwx)

For files: 660 (U+rw, g+rw, a-x, o-rw)

I'd like to try and do this with a single recursive chmod if possible -- as to avoid recursing through each directory and setting file-by-file permissions.

I imagine there's got to be a way to do this without writing a shell script of my own -- but I haven't been able to find anything.

I appreciate your help!


I do find a script useful since it's often useful to change both file and directory permissions in one swoop, and they are often linked. 770 and 660 for shared directories on a file server, 755/644 for web server directories, etc. I keep a script w/ the most commonly used mode for that type of server in root's bin/ and just do the find manually when the common mode doesn't apply.

# syntax: setperm.s destdir
if [ -z $1 ] ; then echo "Requires single argument: <directoryname>" ; exit 1 ;                                       fi




printf "\nThis will RECURSIVELY change the permissions for this entire branch:\n                                      "
printf "\t$destdir\n"
printf "\tDirectories chmod = $dirmode\tFiles chmod = $filemode\n"
printf "Are you sure want to do this [$YN]? "

read YN

case $YN in
        # change permissions on files and directories.
        find $destdir -type f -print0 | xargs -0 chmod $filemode $i
        find $destdir -type d -print0 | xargs -0 chmod $dirmode $ii ;;

        *) echo "\nBetter safe than sorry I always say.\n" ;;
  • Wow! This is exactly what I was looking for. Thank you very much! – Skone Jan 21 '10 at 0:33
  • Hey iPaulo -- can you by chance explain the "$ii" on the line "find $destdir -type d -print0 | xargs -0 chmod $dirmode $ii ;;". I'm not sure I understand why its not just "find $destdir -type d -print0 | xargs -0 chmod $dirmode $i ;;" Thanks! – Skone Jan 21 '10 at 19:35

No need for scripts.

// Directories:

find . -type d -exec chmod XXX {} \;

// Files:

find . -type f -exec chmod XXX {} \;

In your case it might not have to be as complicated as others has made it out to be (though find is indeed a good tool for this sort of thing in general). The difference between the modes are the execute bit. If it is the case that no files already has the execute bit set, then you can do it in a single invocation of chmod, just as you asked.

chmod -R u=rwX,g=rwX,o= FILE...

The key here is the capital X, which the man page explains as

execute/search only if the file is a directory or already has execute permission for some user.

Thus, if your files do not already have the execute bit set, it will only be set for directories.

  • This is great, but much longer than just writing 777. Is there a shorthand for the -X ? – Elliott B Apr 23 '18 at 22:49

I found that for my use case at least, using rsync to copy the directory onto itself was much faster than using chmod directly with a list of files from find.

rsync -rpt --chmod=D770,F660 . .

If you want to add a chown to the same operation, rsync lets you do that too with the --chown=user:group option.

  • Whoah, this actually a lot faster than chmod -R. – Epeli Dec 3 '18 at 17:46
  • So much faster than chmod. Are there any downsides to using rsync this way? I've been doing it a lot and everything seems fine. – slothbear May 12 '20 at 1:36

Clean and simple:

chmod 660 $(find . -type f)
chmod 770 $(find . -type d)
  • Very elegant approach. – user5336 Sep 13 '10 at 17:12
  • 3
    perhaps, but note that this only works if the files have no spaces in them. A more "safe" approach is find ... -exec chmod ... {} \; – isaaclw Jul 18 '12 at 22:30

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.