Skip to content

Instantly share code, notes, and snippets.

@x3rAx
Created September 27, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save x3rAx/26349eda581acf071bbc0b2a14830527 to your computer and use it in GitHub Desktop.
Save x3rAx/26349eda581acf071bbc0b2a14830527 to your computer and use it in GitHub Desktop.
Shell script to easily fix permissions recursively on files and directories (Set user / group / file mode / dir mode)
#!/usr/bin/env bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# MIT License
#
# Copyright (c) 2019 Björn Richter
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
USAGE="
Usage:
./fix-permissions.sh <OWNER|:GROUP|OWNER:GROUP|-> <DIR_MODE|-> <FILE_MODE|-> PATH...
Examples:
Fix permissions for home directory
./fix-permissions.sh tony:stark 755 644 /home/tony/
Fix permissions for specific directories and files and only change user, not group:
./fix-permissions.sh tony 700 600 /home/tony/.ssh/ /home/tony/.my.cnf
Fix only file permissions and change only group (for folders as well as files)
./fix-permissions.sh :stark 755 - /srv/data/tony/
Any of the parameters in angle brackets \`<...>\` can be skipped by passing a
single dash \`-\`. For example, when passing a \`-\` for DIR_MODE, the dir mode
will not be changed.
"
set -e # fail when command failed
if [[ $1 =~ ^(-h|--help)$ ]]; then
echo "$USAGE"
exit
fi
if [[ $# -lt 4 ]]; then
echo >&2 "$USAGE"
exit 1
fi
owner_group="$1"
dir_mode="$2"
file_mode="$3"
paths="${@:4}"
# Fix owner / group
if [[ $owner_group != '-' ]]; then
chown -R "$owner_group" "${paths[@]}"
fi
# Fix directry permissions
if [[ $dir_mode != '-' ]]; then
find "${paths[@]}" -type d -exec chmod "$dir_mode" "{}" +
fi
# Fix file permissions
if [[ $file_mode != '-' ]]; then
find "${paths[@]}" -type f -exec chmod "$file_mode" "{}" +
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment