Aug 19, 2011
tom

CHMOD – Applying Different Permissions For Files vs. Directories

Question

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!

Answer

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.

#!/bin/sh
# syntax: setperm.s destdir
#
if [ -z $1 ] ; then echo "Requires single argument: <directoryname>" ; exit 1 ;                                       fidestdir=$1dirmode=0770
filemode=0660YN=noprintf "nThis will RECURSIVELY change the permissions for this entire branch:n                                      "
printf "t$destdirn"
printf "tDirectories chmod = $dirmodetFiles chmod = $filemoden"
printf "Are you sure want to do this [$YN]? "read YNcase $YN in
        [yY]|[yY][eE][sS])
        # 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" ;;
esac

Related posts:

  1. Change ownership or permissions on only directories or files, recursively
  2. chmod all files (including hidden files) in a directory in Linux (not recursively)
  3. chmod: copy permissions (with extended permissions) from one file to another
  4. chmod of 400,000 files inside multiple subdirectories?
  5. SUDO permissions, file copy/chmod question?

Leave a comment