#!/bin/bash set -e #initalize globals ## prepended space to make final assembly cleaner whitelist_find_str=' \(' blacklist_find_str=' -not \(' search_dir="$PWD" whitelist=() blacklist=() ending='' action=' -print' help_use () { echo "Usage:" echo -e '\tfind_ext.sh [-w ] [-b ] [-[-p] [-c ] [-d]' echo -e '\t\t-w "list of file extensions" - Whitelist: Match files with provided extensions' echo -e '\t\t\texample: find_ext.sh -w "txt pdf doc"' } # use getopts to parse options ## each letter is a flag like -p ## letter with : after is is a flag with an arg like -w "txt pdf gif" ## first : is for when no flags are added (defaults, error handling) while getopts ":w:b:d:c:Dhps]" opt; do case "$opt" in # directory - path to search in (default $PWD) ## find_ext.sh -w "txt jpg" -d /home/princesspi d) search_dir="${OPTARG[@]}" if [ ! -d "$search_dir" ]; then echo "Error: Search directory $search_dir not found" >&2 help_useage exit 1 fi ;; # silent ## find_ext -w "txt mp3" -s s) # show error text but silence everything else ending=' 1>/dev/null' ;; # whitelist - search for files ending in provided extensions ## find_ext.sh -w "txt pdf bak" -p w) ## break whitelist str into array IFS=' ' read -r -a whitelist <<< "${OPTARG[@]}" ### start the whitelist group #### whitelist counter so it knows its in the first position (to not add the -o) whitelist_counter=0 ### loop through array of whitelisted exts for ext in "${whitelist[@]}"; do ### only add -o after the first one if [ $whitelist_counter -eq 0 ]; then #### test if array has exactly one item if [ ${#whitelist[@]} -eq 1 ]; then ##### Append space because its the only entry whitelist_find_str+=" -iname \"*.${ext}\" " else ##### no appended space on first one to make it neat with the -o's whitelist_find_str+=" -iname \"*.${ext}\"" fi else whitelist_find_str+=" -o -iname \"*.${ext}\" " fi ### increment counter whitelist_counter=$(($whitelist_counter + 1)); done ## finish the whitelist group ### appended space to make final assembly cleaner whitelist_find_str+='\) ' ;; # blacklist - exclude provided extensions from search ## find_ext.sh -b "txt pdf jpg" -p b) # break blacklist str into array IFS=' ' read -r -a blacklist <<< "${OPTARG[@]}" echo ${#blacklist[@]} # create the blacklist find section text ## start the whitelist group ## has the -not for blacklisting ## whitelist counter so it knows its in the first position (to not add the -o) blacklist_counter=0 ## loop through array of whitelisted exts for ext in "${blacklist[@]}"; do ### test if array has exactly one item if [ ${#blacklist[@]} -eq 1 ]; then #### Append space because its the only entry blacklist_find_str+=" -iname \"*.${ext}\" " else #### no appended space on first one to make it neat with the -o's blacklist_find_str+=" -iname \"*.${ext}\"" fi ### increment counter blacklist_counter=$(($whitelist_counter + 1)); done ## finish the whitelist group ### appended space to make final assembly cleaner blacklist_find_str+=' \) ' ;; # print (default( ## find_ext.sh "jpg gif png" -p p) # do nothing here, just do the default print silently # action=' -print' ;; # delete ## find_ext.sh -w "tmp bak" -d D) # verify that user REALLY wants to delete # todo: show list of files before prompting to confirm # send warning as stderr to bypass s echo -e "ARE YOU CERTAIN YOU WISH TO DELETE ALL MATCHING FILES RECURSIVELY IN $search_dir?\n\tPatterns:\n\t\tWhitelist: \"${whitelist[@]}\"\n\t\tBlacklist: \"${blacklist[@]}\"\n\t\tPath: $search_dir" >&2 read checkdelete # strict case insensitive yes match, no other chars, nothing prepended or appendeed, no doubles, no mispellling regex="^[yY]{1}[eE]{1}[sS]{1}$" if [[ $checkdelete =~ $regex ]]; then echo "WARNING PROCEEDING TO DELETE!" else echo "NOTE: DELETE CANCELLED" exit 0 fi action+=' -delete' ;; # copy ## find_ext.sh -w "txt" -c /path/somewhere c) action+=" -exec cp \"{}\" \"${OPTARG[@]}\" \\;" ;; # help ## find_ext.sh -h h) help_usage exit 0 ;; # invalid options or flags \?) echo "Error: Invalid Option -$OPTARG" >&2 help_usage exit 0; ;; # arg requiring value not given ## like -w without the list of exts :) echo "Error: Option -$OPTARG requires an argument" >&2 help_usage exit 1 ;; esac done # assemble the find command ## "$PWD" used for dir instead of . to output absolute paths find_str="find \"$search_dir\"${whitelist_find_str}${blacklist_find_str}${action}${ending}"; echo $find_str # bash -c "$find_str" exit 0 if [ -z "$1" ]; then echo -e 'Usage:\n\tfind_ext.sh \n\tfind_ext.sh "jpg mp3 png" ["doc pdf svg"]' exit 1 else # break whitelist str into array IFS=' ' read -r -a whitelist <<< "$1" # create the whitelisted find section text ## start the whitelist group ## prepended space to make final assembly cleaner whitelist_find_str=' \(' ## whitelist counter so it knows its in the first position (to not add the -o) whitelist_counter=0 ## loop through array of whitelisted exts for ext in "${whitelist[@]}"; do ### only add -o after the first one if [ $whitelist_counter -eq 0 ]; then #### test if array has exactly one item if [ ${#whitelist[@]} -eq 1 ]; then ##### Append space because its the only entry whitelist_find_str+=" -iname \"*.${ext}\" " else ##### no appended space on first one to make it neat with the -o's whitelist_find_str+=" -iname \"*.${ext}\"" fi else whitelist_find_str+=" -o -iname \"*.${ext}\" " fi ### increment counter whitelist_counter=$(($whitelist_counter + 1)); done ## finish the whitelist group ### appended space to make final assembly cleaner whitelist_find_str+='\) ' fi if [ ! -z "$2" ]; then # break blacklist str into array IFS=' ' read -r -a blacklist <<< "$2" # create the blacklist find section text ## start the whitelist group ## has the -not for blacklisting ## prepended space to make final assembly cleaner blacklist_find_str=' -not \(' ## whitelist counter so it knows its in the first position (to not add the -o) blacklist_counter=0 ## loop through array of whitelisted exts for ext in "${blacklist[@]}"; do ### only add -o after the first one if [ $blacklist_counter -eq 0 ]; then #### test if array has exactly one item if [ ${#blacklist[@]} -eq 1 ]; then ##### Append space because its the only entry blacklist_find_str+=" -iname \"*.${ext}\" " else ##### no appended space on first one to make it neat with the -o's blacklist_find_str+=" -iname \"*.${ext}\"" fi else blaclklist_find_str+=" -o -iname \"*.${ext}\" " fi ### increment counter blacklist_counter=$(($whitelist_counter + 1)); done ## finish the whitelist group ### appended space to make final assembly cleaner blacklist_find_str+='\) ' fi