-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrepo-sizer.sh
More file actions
219 lines (204 loc) · 7.45 KB
/
repo-sizer.sh
File metadata and controls
219 lines (204 loc) · 7.45 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
################################################
# Script to Traverse Repo and find Large files #
# Will give report of files over size limit #
# Will give report of files with #
# particular extensions #
# #
# @AdmiralAwkbar #
################################################
#
# Legend:
# To run this script, you just need:
# - chmod +x script.sh
# - ./script.sh <path/to/scan>
#
# Script will scan that directory for size and
# files with extensions that could be ommitted
#
########
# VARS #
########
DIR_TO_SCAN=$1 # Directory to scan for large files
SIZE_LIMIT='100' # Size in MB to look for
SIZE_LIMIT+="M" # Add the M to the end for megabytes. Options include k,M,T,P
FILE_TYPES=(".jar" ".war" ".zip" ".gzip" ".obj") # List of file types to find and warn on
ERROR_COUNT='0' # Total errors found
################################################################################
############################ FUNCTIONS #########################################
################################################################################
################################################################################
#### Function ValidateInput ####################################################
ValidateInput()
{
##################################
# Validate we have a dir to scan #
##################################
if [ $# -lt 1 ]; then
# Send it to help screen
echo "ERROR! Please give directory to search for large files"
echo "Example: $0 /tmp/myRepo"
echo "-----------------------------------------------------"
exit 1
fi
}
################################################################################
#### Function Header ###########################################################
Header()
{
#####################
# Print Header Info #
#####################
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo "--------------- Repo Size Scanner -------------------"
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo ""
echo "Scanning Directory:[$DIR_TO_SCAN]"
echo "Script will scan directory recursively to find files"
echo "over the size limit:[$SIZE_LIMIT]mb"
echo "Script will report files over the limit, as well as "
echo "any files found with the following extensions:"
for TYPE in "${FILE_TYPES[@]}"; do
echo "Extension:[$TYPE]"
done
echo ""
}
################################################################################
#### Function ValidateDirectory ################################################
ValidateDirectory()
{
########################################################
# Checking that the directory exists and we can see it #
########################################################
echo "-----------------------------------------------------"
if [ -d "$DIR_TO_SCAN" ]; then
echo "Found directory, preparing for scan..."
else
echo "ERROR! Could not find Directory:[$DIR_TO_SCAN]"
exit 1
fi
}
################################################################################
#### Function GetRepoSize ######################################################
GetRepoSize()
{
################################
# Get the size on disk or repo #
################################
echo "-----------------------------------------------------"
echo "Getting complete size of repository on disk."
echo "This could take several moments depending on repo size..."
# Grab the current size of the repository on disk
SIZE=($(du -sh $DIR_TO_SCAN))
# Print the size thats cleaned up
echo "Total size of repository on disk:[$SIZE]"
}
################################################################################
#### Function RunScan ##########################################################
RunScan()
{
echo "-----------------------------------------------------"
echo "Running scan of:[$DIR_TO_SCAN]"
echo "This could take several moments depending on repo size..."
#############################################
# Print the list of files that are an issue #
#############################################
echo "-----------------------------------------------------"
echo "---- Files that were found over the size limit: ----"
echo "-----------------------------------------------------"
# Save current IFS
SAVEIFS=$IFS
# Change IFS to new line.
IFS=$'\n'
OVER_LIMIT=($(find $DIR_TO_SCAN -type f -size +$SIZE_LIMIT -exec du -h {} \; | sort -n))
# Restore IFS
IFS=$SAVEIFS
#################################
# Check the results of the call #
#################################
if [ ${#OVER_LIMIT[@]} -eq 0 ]; then
echo "0 files found over limit"
else
for FILE in "${OVER_LIMIT[@]}"; do
echo "[$FILE]"
((ERROR_COUNT++))
done
fi
}
################################################################################
#### Function ScanWhitelist ####################################################
ScanWhitelist()
{
echo "-----------------------------------------------------"
echo "Running scan of:[$DIR_TO_SCAN] for file types:"
echo "This could take several moments depending on repo size..."
#############################################
# Print the list of files that are an issue #
#############################################
for TYPE in "${FILE_TYPES[@]}"; do
echo "--------------------------"
echo "Searching for type:[$TYPE]"
# Need to load files found into array
FILES_FOUND=($(find $DIR_TO_SCAN -name "*$TYPE"))
if [ ${#FILES_FOUND[@]} -eq 0 ]; then
echo "0 files found"
else
for FILE in "${FILES_FOUND[@]}"; do
echo "Found File:[$FILE]"
((ERROR_COUNT++))
done
fi
done
}
################################################################################
#### Function Footer ###########################################################
Footer()
{
######################
# Print Closing Info #
######################
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
if [ $ERROR_COUNT -eq 0 ]; then
echo "Process Completed Successfully"
echo "No files over size limit or bad extensions"
echo "-----------------------------------------------------"
else
echo "ERRORS FOUND! COUNT:[$ERROR_COUNT]"
echo "-----------------------------------------------------"
exit $ERROR_COUNT
fi
}
################################################################################
############################## MAIN ############################################
################################################################################
##################
# Validate Input #
##################
ValidateInput $1
###########
# Headers #
###########
Header
#################################
# Validate the Directory Exists #
#################################
ValidateDirectory
#################################
# Get the size of the full repo #
#################################
GetRepoSize
###############
# Check files #
###############
RunScan
#########################
# Check Whitelist files #
#########################
ScanWhitelist
################
# Print Footer #
################
Footer