Skip to content

Instantly share code, notes, and snippets.

@mfurlend
Created January 6, 2016 19:55
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 mfurlend/a1c7950432e7a9442eb1 to your computer and use it in GitHub Desktop.
Save mfurlend/a1c7950432e7a9442eb1 to your computer and use it in GitHub Desktop.
A convenience wrapper around setfacl to facilitate sharing a file or directory with a user or group.
# A convenience wrapper around setfacl to facilitate sharing a file or directory with a user or group.
# ex:
# $ getfacl ~/test_directory
# file: /root/test_directory
# owner: root
# group: root
# user::rwx
# group::r-x
# other::r-x
#
# $ share_with -u apache ~/test_directory
#
# $ getfacl ~/test_directory
# file: /root/test_directory
# owner: root
# group: root
# user::rwx
# user:apache:rwx
# group::r-x
# mask::rwx
# other::r-x
# default:user::rwx
# default:user:apache:rwx
# default:group::r-x
# default:mask::rwx
# default:other::r-x
share_with() {
ug='u'
while getopts ":u:g" opt; do
case $opt in
u)
ug='u'
shift
;;
g)
ug='g'
shift
;;
esac
done
if [ "$#" -ne 2 ]; then
echo "Usage: $0 -u [USER] [PATH]" >&2
echo "Usage: $0 -g [GROUP] [PATH]" >&2
return 1
fi
if ! [ -e "$2" ]; then
echo "$2 is not a file or directory" >&2
return 1
fi
[ -d $2 ] && setfacl -Rm ${ug}:${1}:rwX,d:${ug}:${1}:rwX ${2}
[ -f $2 ] && setfacl -m ${ug}:${1}:rwX ${2}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment