-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixhomeperms.sh
executable file
·146 lines (122 loc) · 4.04 KB
/
fixhomeperms.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
#
# fixhomeperms.sh v0.0001
#
# quick and dirty script to fix permissions for all files and directories under $HOME
#
# the following is assumed to be true for the final results:
# * directories: rwxr-x-r-x (octal: 0755)
# * regular files: rw-r--r-- (octal: 0644)
# * executable files: rwxr-xr-x (octal: 0755)
# - Shell scripts (.sh)
# - Perl files (.pl)
# - Python files (.py)
# - Ruby files (.rb)
# - RUN files (.run)
# -------------------------------------------
#
# Functions
#
usage() # option -h
{
echo "$(basename $0) by Martin Mielke <martinm@rsysadmin.com>"
echo -e "\nUsage:\t$(basename $0) [-h] [-d] [-n] [-s] [-p] [-P] [-r] [-R] [-a]"
echo -e "\t-h Prints this help."
echo -e "\t-d Fix permissions on directories (0755)."
echo -e "\t-n Fix permissions on non-exec files (0644)."
echo -e "\t-s Fix permissions on shell script files (0755)."
echo -e "\t-p Fix permissions on Perl files (0755)."
echo -e "\t-P Fix permissions on Python files (0755)."
echo -e "\t-r Fix permissions on Ruby files (0755)."
echo -e "\t-R Fix permissions on RUN files (0755)."
echo -e "\t-a Fix permissions on all executable files (.sh, .pl, .py, .rb, .run)."
echo -e "\t-A Fix all in one step, including directories and non-exec files."
exit 1
}
function returnStatus { # helper funcion
if [[ $? -eq 0 ]]
then
echo -e "\t[ OK ]"
else
echo -e "\t[ ERROR ]"
fi
}
function finishMessage {
echo -e " [$(date +%_H:%M:%S)] -- All set.\n"
exit 0
}
function fixDirectories { # option -d
echo -e " [$(date +%_H:%M:%S)] == Fixing directory permissions (chmod 0755) ... \c"
find $HOME/ -type d -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixNonExecFiles { # option -n
echo -e " [$(date +%_H:%M:%S)] == Fixing non-exec files permissions (chmod 0644) ... \c"
find $HOME/ -type f \( -not -name '*.sh' -not -name '*.pl' -not -name '*.py' -not -name '*.rb' -not -name '*.run' -not -path '*/\.*' \) -print0 | xargs -0 -n1024 -P0 chmod 0644
returnStatus
}
function fixShellFiles { # option -s
echo -e "[$(date +%_H:%M:%S)] == Fixing permissions for Shell scripts... \c"
find $HOME/ -type f -name '*.sh' -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixPerlFiles { # option -p
echo -e "[$(date +%_H:%M:%S)] == Fixing permissions for Perl files... \c"
find $HOME/ -type f -name '*.pl' -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixPythonFiles { # option -P
echo -e "[$(date +%_H:%M:%S)] == Fixing permissions for Python files... \c"
find $HOME/ -type f -name '*.py' -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixRubyFiles { # option -r
echo -e "[$(date +%_H:%M:%S)] == Fixing permissions for Ruby files... \c"
find $HOME/ -type f -name '*.rb' -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixRunFiles { # option -R
echo -e "[$(date +%_H:%M:%S)] == Fixing permissions for RUN files... \c"
find $HOME/ -type f -name '*.run' -print0 | xargs -0 -n1024 -P0 chmod 0755
returnStatus
}
function fixAllExecutableFiles { # option -a
fixShellFiles
fixPerlFiles
fixPythonFiles
fixRubyFiles
fixRunFiles
}
function fixAll { # option -A
fixDirectories
fixNonExecFiles
fixAllExecutableFiles
}
# main()
if [[ $# -eq 0 ]]
then
usage
fi
set -- `getopt hdnspPrRaA $*`
if [[ $? -ne 0 ]]
then
usage
fi
for arg in $*
do
case $arg in
-h) usage; shift 1;;
-d) fixDirectories; exit 0;;
-n) fixNonExecFiles; exit 0;;
-s) fixShellFiles; exit 0;;
-p) fixPerlFiles; exit 0;;
-P) fixPythonFiles; exit 0;;
-r) fixRubyFiles; exit 0;;
-R) fixRunFiles; exit 0;;
-a) fixAllExecutableFiles; exit 0;;
-A) fixAll; exit 0;;
*) usage;;
esac
done
finishMessage
# The End.