Skip to content

Instantly share code, notes, and snippets.

@ronisaha
Last active January 15, 2017 04:36
Show Gist options
  • Select an option

  • Save ronisaha/4c197dc6d374960a3f99 to your computer and use it in GitHub Desktop.

Select an option

Save ronisaha/4c197dc6d374960a3f99 to your computer and use it in GitHub Desktop.
Set folder permision to web-server user
#!/bin/bash
E_BAD_ARGS=85
E_BAD_PATH=-1
if [ ! -n "$1" ]
then
echo "Usage: $0 dir1 dir2 etc."
exit ${E_BAD_ARGS}
fi
ALL_DIRECTORIES=${@%/}
#Get the user name for webserver
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | grep -v apache- | head -1 | cut -d\ -f1`
echo
#Make sure all directories exists
for arg in "$@"
do
DIRECTORY_PATH=${arg%/}
if [[ "${DIRECTORY_PATH}" = /* ]]
then
echo "Only relative path are allowed"
exit ${E_BAD_PATH}
else
#echo "Creating : ${DIRECTORY_PATH}"
mkdir -p "${DIRECTORY_PATH}"
fi
done
echo "Changing permission for : ${ALL_DIRECTORIES}"
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX ${ALL_DIRECTORIES}
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX ${ALL_DIRECTORIES}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment