Skip to content

Instantly share code, notes, and snippets.

@gilbarbara
Last active November 25, 2015 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbarbara/f3485fafc17dd33de46f to your computer and use it in GitHub Desktop.
Save gilbarbara/f3485fafc17dd33de46f to your computer and use it in GitHub Desktop.
Change files and directories permissions recursively.
#!/bin/sh
#
# chmodr.sh
#
usage()
{
echo "Usage: $0 [-d DIRPERMS] [-f FILEPERMS] PATH"
exit 1
}
while getopts ":d:f:" o; do
case "${o}" in
d)
DIRPERMS=${OPTARG}
;;
f)
FILEPERMS=${OPTARG}
;;
\?)
usage
;;
esac
done
shift $(($OPTIND - 1))
# Default directory permissions
if [ -z "$DIRPERMS" ] ; then
DIRPERMS=755
fi
# Default file permissions
if [ -z "$FILEPERMS" ] ; then
FILEPERMS=644
fi
# Set the root path
ROOT=$1
if [ -z $1 ] ; then
ROOT='.'
fi
# root path is a valid directory
if [ ! -d $ROOT ] ; then
echo "$ROOT does not exist or isn't a directory!" ; exit 1
fi
# set directory permissions recursively
if [ -n "$DIRPERMS" ] ; then
find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS
fi
# set file permissions recursively
if [ -n "$FILEPERMS" ] ; then
find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS
fi
@gilbarbara
Copy link
Author

Save this file to /usr/local/bin, remove .sh the suffix and chmod 755 /usr/local/bin/chmodr
Run with: chmodr -d 775 -f 664 /path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment