Last active
August 29, 2015 14:06
-
-
Save sndsgd/4199f9d4c920d44f396f to your computer and use it in GitHub Desktop.
Set permissions for a web server document root directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dir=$1 | |
user=$2 | |
err=() | |
if [ "$UID" -ne 0 ]; then | |
err+=("this script must be run as root") | |
fi | |
if [ -z "$dir" ]; then | |
err+=("a directory was not specified") | |
elif [ ! -d "$dir" ]; then | |
err+=("invalid argument provided for directory") | |
fi | |
if [ -z "$user" ]; then | |
err+=("a user was not specified") | |
elif [ "$(getent passwd $user)" = "" ];then | |
err+=("user '"$user"' could not be found") | |
fi | |
errcnt=${#err[*]} | |
if [ "$errcnt" -eq 0 ]; then | |
echo "updating ownership..." | |
chown -R $user:$user "$dir" || { echo 'failed'; exit 1; } | |
echo "changing directory permissions..." | |
chmod 2775 "$dir"|| { echo 'failed' ; exit 1; } | |
echo "finding and updating child directory permissions..." | |
find "$dir" -type d -exec chmod 2775 {} + || { echo 'failed'; exit 1; } | |
echo "finding and updating child file permissions..." | |
find "$dir" -type f -exec chmod 0664 {} + || { echo 'failed'; exit 1; } | |
echo "done" | |
exit 0 | |
fi | |
[[ "$errcnt" = 1 ]] && msg="an error" || msg="the following errors" | |
echo $msg" occured" | |
printf -- ' - %s\n' "${err[@]}" | |
echo " | |
Usage: "$(basename "$0")" [directory] [username] | |
Set suitable permissions for a webserver directory | |
Arguments: | |
directory an absolute path to a directory | |
username the name of the user" | |
exit 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment