1 # Some useful commands for setting permissions.
3 # Rory Hardy [GneatGeek]
4 # Andrew Janke [apjanke]
8 # Set all files' permissions to 644 recursively in a directory
10 find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644
13 # Set all directories' permissions to 755 recursively in a directory
15 find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755
20 # fixperms - fix permissions on files and directories, with confirmation
21 # Returns 0 on success, nonzero if any errors occurred
23 local opts confirm target exit_status chmod_opts use_slow_mode
24 zparseopts -E -D -a opts -help -slow v+=chmod_opts
25 if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
27 Usage: fixperms [-v] [--help] [--slow] [target]
29 target is the file or directory to change permissions on. If omitted,
30 the current directory is taken to be the target.
32 -v enables verbose output (may be supplied multiple times)
34 --slow will use a slower but more robust mode, which is effective if
35 directories themselves have permissions that forbid you from
39 exit_status=$(( $# > 1 ))
43 if [[ $# == 0 ]]; then
48 if [[ -n ${opts[(r)--slow]} ]]; then use_slow=true; else use_slow=false; fi
50 # Because this requires confirmation, bail in noninteractive shells
51 if [[ ! -o interactive ]]; then
52 echo "fixperms: cannot run in noninteractive shell"
56 echo "Fixing perms on $target?"
57 printf '%s' "Proceed? (y|n) "
59 if [[ "$confirm" != y ]]; then
64 # This xargs form is faster than -exec chmod <N> {} \; but will encounter
65 # issues if the directories themselves have permissions such that you can't
66 # recurse in to them. If that happens, just rerun this a few times.
68 if [[ $use_slow == true ]]; then
69 # Process directories first so non-traversable ones are fixed as we go
70 find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \;
71 if [[ $? != 0 ]]; then exit_status=$?; fi
72 find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \;
73 if [[ $? != 0 ]]; then exit_status=$?; fi
75 find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755
76 if [[ $? != 0 ]]; then exit_status=$?; fi
77 find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644
78 if [[ $? != 0 ]]; then exit_status=$?; fi