Skip to content

Instantly share code, notes, and snippets.

@simonrad
Created July 1, 2013 00:34
Show Gist options
  • Select an option

  • Save simonrad/5897664 to your computer and use it in GitHub Desktop.

Select an option

Save simonrad/5897664 to your computer and use it in GitHub Desktop.
Script to grant R/W permissions to staff
#!/bin/bash
# --------------------------------------------------
# This script grants READ AND WRITE permissions to staff, for the specified directory.
# Takes 1 parameter ($1): A directory.
# Recursively changes the directory to have group=staff,
# and adds read and write permissions for that group.
# Also adds execute permissions for all directories (but not files).
# --------------------------------------------------
# Make the script terminate if any line fails.
set -e
# Parse command line options.
# "-y" means don't confirm changes.
DO_CONFIRM=true
while getopts y: option
do
case "${option}"
in
y) DO_CONFIRM=false; shift 1;;
esac
done
# Check number of command-line args.
if [ $# != 1 ]
then
echo "Incorrect number of arguments. One argument expected (a directory)."
exit 1
fi
echo "Changing group permissions for $1"
if [ $DO_CONFIRM == false ]
then
REPLY=y
else
read -p "Do you want to continue? (y/n) " -n 1 -r
echo
fi
if [[ $REPLY =~ ^[Yy]$ ]]
then
# The meat of the script.
chgrp -R staff $1
chmod -R g+rw $1
# This finds only the directories.
find $1 -type d -print0 | xargs -0 chmod g+x
echo "Finished changing group permissions for $1"
else
echo "Cancelled changing group permissions"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment