|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +################################################ |
| 4 | +# Script to Traverse Repo and find Large files # |
| 5 | +# Will give report of files over size limit # |
| 6 | +# Will give report of files with # |
| 7 | +# particular extensions # |
| 8 | +# # |
| 9 | +# @AdmiralAwkbar # |
| 10 | +################################################ |
| 11 | + |
| 12 | +# |
| 13 | +# Legend: |
| 14 | +# To run this script, you just need: |
| 15 | +# - chmod +x script.sh |
| 16 | +# - ./script.sh <path/to/scan> |
| 17 | +# |
| 18 | +# Script will scan that directory for size and |
| 19 | +# files with extensions that could be ommitted |
| 20 | +# |
| 21 | + |
| 22 | +######## |
| 23 | +# VARS # |
| 24 | +######## |
| 25 | +DIR_TO_SCAN=$1 # Directory to scan for large files |
| 26 | +SIZE_LIMIT='100' # Size in MB to look for |
| 27 | +SIZE_LIMIT+="M" # Add the M to the end for megabytes. Options include k,M,T,P |
| 28 | +FILE_TYPES=(".jar" ".war" ".zip" ".gzip" ".obj") # List of file types to find and warn on |
| 29 | +ERROR_COUNT='0' # Total errors found |
| 30 | + |
| 31 | +################################################################################ |
| 32 | +############################ FUNCTIONS ######################################### |
| 33 | +################################################################################ |
| 34 | +################################################################################ |
| 35 | +#### Function ValidateInput #################################################### |
| 36 | +ValidateInput() |
| 37 | +{ |
| 38 | + |
| 39 | + ################################## |
| 40 | + # Validate we have a dir to scan # |
| 41 | + ################################## |
| 42 | + if [ $# -lt 1 ]; then |
| 43 | + # Send it to help screen |
| 44 | + echo "ERROR! Please give directory to search for large files" |
| 45 | + echo "Example: $0 /tmp/myRepo" |
| 46 | + echo "-----------------------------------------------------" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +} |
| 50 | +################################################################################ |
| 51 | +#### Function Header ########################################################### |
| 52 | +Header() |
| 53 | +{ |
| 54 | + ##################### |
| 55 | + # Print Header Info # |
| 56 | + ##################### |
| 57 | + echo "-----------------------------------------------------" |
| 58 | + echo "-----------------------------------------------------" |
| 59 | + echo "--------------- Repo Size Scanner -------------------" |
| 60 | + echo "-----------------------------------------------------" |
| 61 | + echo "-----------------------------------------------------" |
| 62 | + echo "" |
| 63 | + echo "Scanning Directory:[$DIR_TO_SCAN]" |
| 64 | + echo "Script will scan directory recursively to find files" |
| 65 | + echo "over the size limit:[$SIZE_LIMIT]mb" |
| 66 | + echo "Script will report files over the limit, as well as " |
| 67 | + echo "any files found with the following extensions:" |
| 68 | + for TYPE in "${FILE_TYPES[@]}"; do |
| 69 | + echo "Extension:[$TYPE]" |
| 70 | + done |
| 71 | + echo "" |
| 72 | +} |
| 73 | +################################################################################ |
| 74 | +#### Function ValidateDirectory ################################################ |
| 75 | +ValidateDirectory() |
| 76 | +{ |
| 77 | + ######################################################## |
| 78 | + # Checking that the directory exists and we can see it # |
| 79 | + ######################################################## |
| 80 | + echo "-----------------------------------------------------" |
| 81 | + if [ -d "$DIR_TO_SCAN" ]; then |
| 82 | + echo "Found directory, preparing for scan..." |
| 83 | + else |
| 84 | + echo "ERROR! Could not find Directory:[$DIR_TO_SCAN]" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +} |
| 88 | +################################################################################ |
| 89 | +#### Function GetRepoSize ###################################################### |
| 90 | +GetRepoSize() |
| 91 | +{ |
| 92 | + ################################ |
| 93 | + # Get the size on disk or repo # |
| 94 | + ################################ |
| 95 | + echo "-----------------------------------------------------" |
| 96 | + echo "Getting complete size of repository on disk." |
| 97 | + echo "This could take several moments depending on repo size..." |
| 98 | + # Grab the current size of the repository on disk |
| 99 | + SIZE=($(du -sh $DIR_TO_SCAN)) |
| 100 | + |
| 101 | + # Print the size thats cleaned up |
| 102 | + echo "Total size of repository on disk:[$SIZE]" |
| 103 | +} |
| 104 | +################################################################################ |
| 105 | +#### Function RunScan ########################################################## |
| 106 | +RunScan() |
| 107 | +{ |
| 108 | + echo "-----------------------------------------------------" |
| 109 | + echo "Running scan of:[$DIR_TO_SCAN]" |
| 110 | + echo "This could take several moments depending on repo size..." |
| 111 | + |
| 112 | + ############################################# |
| 113 | + # Print the list of files that are an issue # |
| 114 | + ############################################# |
| 115 | + echo "-----------------------------------------------------" |
| 116 | + echo "---- Files that were found over the size limit: ----" |
| 117 | + echo "-----------------------------------------------------" |
| 118 | + # Save current IFS |
| 119 | + SAVEIFS=$IFS |
| 120 | + # Change IFS to new line. |
| 121 | + IFS=$'\n' |
| 122 | + OVER_LIMIT=($(find $DIR_TO_SCAN -type f -size +$SIZE_LIMIT -exec du -h {} \; | sort -n)) |
| 123 | + # Restore IFS |
| 124 | + IFS=$SAVEIFS |
| 125 | + ################################# |
| 126 | + # Check the results of the call # |
| 127 | + ################################# |
| 128 | + if [ ${#OVER_LIMIT[@]} -eq 0 ]; then |
| 129 | + echo "0 files found over limit" |
| 130 | + else |
| 131 | + for FILE in "${OVER_LIMIT[@]}"; do |
| 132 | + echo "[$FILE]" |
| 133 | + ((ERROR_COUNT++)) |
| 134 | + done |
| 135 | + fi |
| 136 | +} |
| 137 | +################################################################################ |
| 138 | +#### Function ScanWhitelist #################################################### |
| 139 | +ScanWhitelist() |
| 140 | +{ |
| 141 | + echo "-----------------------------------------------------" |
| 142 | + echo "Running scan of:[$DIR_TO_SCAN] for file types:" |
| 143 | + echo "This could take several moments depending on repo size..." |
| 144 | + |
| 145 | + ############################################# |
| 146 | + # Print the list of files that are an issue # |
| 147 | + ############################################# |
| 148 | + for TYPE in "${FILE_TYPES[@]}"; do |
| 149 | + echo "--------------------------" |
| 150 | + echo "Searching for type:[$TYPE]" |
| 151 | + # Need to load files found into array |
| 152 | + FILES_FOUND=($(find $DIR_TO_SCAN -name "*$TYPE")) |
| 153 | + if [ ${#FILES_FOUND[@]} -eq 0 ]; then |
| 154 | + echo "0 files found" |
| 155 | + else |
| 156 | + for FILE in "${FILES_FOUND[@]}"; do |
| 157 | + echo "Found File:[$FILE]" |
| 158 | + ((ERROR_COUNT++)) |
| 159 | + done |
| 160 | + fi |
| 161 | + done |
| 162 | +} |
| 163 | +################################################################################ |
| 164 | +#### Function Footer ########################################################### |
| 165 | +Footer() |
| 166 | +{ |
| 167 | + ###################### |
| 168 | + # Print Closing Info # |
| 169 | + ###################### |
| 170 | + echo "-----------------------------------------------------" |
| 171 | + echo "-----------------------------------------------------" |
| 172 | + if [ $ERROR_COUNT -eq 0 ]; then |
| 173 | + echo "Process Completed Successfully" |
| 174 | + echo "No files over size limit or bad extensions" |
| 175 | + echo "-----------------------------------------------------" |
| 176 | + else |
| 177 | + echo "ERRORS FOUND! COUNT:[$ERROR_COUNT]" |
| 178 | + echo "-----------------------------------------------------" |
| 179 | + exit $ERROR_COUNT |
| 180 | + fi |
| 181 | +} |
| 182 | +################################################################################ |
| 183 | +############################## MAIN ############################################ |
| 184 | +################################################################################ |
| 185 | + |
| 186 | +################## |
| 187 | +# Validate Input # |
| 188 | +################## |
| 189 | +ValidateInput $1 |
| 190 | + |
| 191 | +########### |
| 192 | +# Headers # |
| 193 | +########### |
| 194 | +Header |
| 195 | + |
| 196 | +################################# |
| 197 | +# Validate the Directory Exists # |
| 198 | +################################# |
| 199 | +ValidateDirectory |
| 200 | + |
| 201 | +################################# |
| 202 | +# Get the size of the full repo # |
| 203 | +################################# |
| 204 | +GetRepoSize |
| 205 | + |
| 206 | +############### |
| 207 | +# Check files # |
| 208 | +############### |
| 209 | +RunScan |
| 210 | + |
| 211 | +######################### |
| 212 | +# Check Whitelist files # |
| 213 | +######################### |
| 214 | +ScanWhitelist |
| 215 | + |
| 216 | +################ |
| 217 | +# Print Footer # |
| 218 | +################ |
| 219 | +Footer |
0 commit comments