From: Steven Baltakatei Sandoval Date: Thu, 1 Dec 2022 06:50:14 +0000 (+0000) Subject: feat(user/list_large_dirs.sh):Add script X-Git-Tag: 0.6.0~5 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/a7d28ffd3bc4de2d16b5d2938fc2a3aaa5f592ad?ds=sidebyside;hp=-c feat(user/list_large_dirs.sh):Add script --- a7d28ffd3bc4de2d16b5d2938fc2a3aaa5f592ad diff --git a/user/list_large_dirs.sh b/user/list_large_dirs.sh new file mode 100755 index 0000000..240ba3c --- /dev/null +++ b/user/list_large_dirs.sh @@ -0,0 +1,264 @@ +#!/usr/bin/env bash +# Desc: Lists directories immediately containing large amounts of data +# Usage: script.sh [int megabytes] [dir] +# Example: A directory named `vacation_photos` located somewhere in $HOME/ +# containing more than 100MB of files (without checking subdirectories of +# `vacation_photos`) could be found by running: +# $ script.sh 100 "$HOME" +# /home/johndoe/Pictures/unsorted/notjunk/vacation_photos +# Version: 0.0.1 + +declare -Ag appRollCall # Associative array for storing app status +declare -Ag fileRollCall # Associative array for storing file status +declare -Ag dirRollCall # Associative array for storing dir status + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +checkInt() { + # Desc: Checks if arg is integer + # Usage: checkInt arg + # Input: arg: integer + # Output: - return code 0 (if arg is integer) + # - return code 1 (if arg is not integer) + # Example: if ! checkInt $arg; then echo "not int"; fi; + # Version: 0.0.1 + local returnState + + #===Process Arg=== + if [[ $# -ne 1 ]]; then + die "ERROR:Invalid number of arguments:$#"; + fi; + + RETEST1='^[0-9]+$'; # Regular Expression to test + if [[ ! $1 =~ $RETEST1 ]] ; then + returnState="false"; + else + returnState="true"; + fi; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Checks if arg is integer +checkapp() { + # Desc: If arg is a command, save result in assoc array 'appRollCall' + # Usage: checkapp arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'appRollCall' + # Output: adds/updates key(value) to global assoc array 'appRollCall' + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command + appRollCall[$arg]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + appRollCall[$arg]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that app exists +checkfile() { + # Desc: If arg is a file path, save result in assoc array 'fileRollCall' + # Usage: checkfile arg1 arg2 arg3 ... + # Version: 0.1.2 + # Input: global assoc. array 'fileRollCall' + # Output: adds/updates key(value) to global assoc array 'fileRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -f "$arg" ]; then + fileRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + elif [ -z "$arg" ]; then + fileRollCall["(no name)"]="false"; returnState="false"; + else + fileRollCall["$arg"]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that file exists +checkdir() { + # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' + # Usage: checkdir arg1 arg2 arg3 ... + # Version 0.1.2 + # Input: global assoc. array 'dirRollCall' + # Output: adds/updates key(value) to global assoc array 'dirRollCall'; + # Output: returns 0 if all args are dirs; 1 otherwise + # Depends: Bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -z "$arg" ]; then + dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false"; + elif [ -d "$arg" ]; then + dirRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi + else + dirRollCall["$arg"]="false"; returnState="false"; + fi + done + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi +} # Check that dir exists +displayMissing() { + # Desc: Displays missing apps, files, and dirs + # Usage: displayMissing + # Version 1.0.0 + # Input: associative arrays: appRollCall, fileRollCall, dirRollCall + # Output: stderr: messages indicating missing apps, file, or dirs + # Output: returns exit code 0 if nothing missing; 1 otherwise + # Depends: bash 5, checkAppFileDir() + local missingApps value appMissing missingFiles fileMissing + local missingDirs dirMissing + + #==BEGIN Display errors== + #===BEGIN Display Missing Apps=== + missingApps="Missing apps :"; + #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done + for key in "${!appRollCall[@]}"; do + value="${appRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing apps: $key => $value"; + missingApps="$missingApps""$key "; + appMissing="true"; + fi; + done; + if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingApps" 1>&2; + fi; + unset value; + #===END Display Missing Apps=== + + #===BEGIN Display Missing Files=== + missingFiles="Missing files:"; + #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + for key in "${!fileRollCall[@]}"; do + value="${fileRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing files: $key => $value"; + missingFiles="$missingFiles""$key "; + fileMissing="true"; + fi; + done; + if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingFiles" 1>&2; + fi; + unset value; + #===END Display Missing Files=== + + #===BEGIN Display Missing Directories=== + missingDirs="Missing dirs:"; + #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + for key in "${!dirRollCall[@]}"; do + value="${dirRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing dirs: $key => $value"; + missingDirs="$missingDirs""$key "; + dirMissing="true"; + fi; + done; + if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. + echo "$missingDirs" 1>&2; + fi; + unset value; + #===END Display Missing Directories=== + + #==END Display errors== + #==BEGIN Determine function return code=== + if [ "$appMissing" == "true" ] || [ "$fileMissing" == "true" ] || [ "$dirMissing" == "true" ]; then + return 1; + else + return 0; + fi + #==END Determine function return code=== +} # Display missing apps, files, dirs +check_depends() { + local flag_return=0; + + if ! checkapp du; then flag_return="1"; fi; + return "$flag_return"; +}; # returns 1 if missing dependencies +size_dir_contents() { + # usage: size_dir_contents [path] + # depends: find, du + local bytes re; + + bytes=0; + re="^[0-9]+$"; + + #yell "DEBUG:Checking dir:$1"; + while read -r addend; do + #yell "DEBUG:addend:$addend"; + if [[ $addend =~ $re ]]; then + bytes="$((bytes + addend))"; + fi; + done < <( find "$1" -maxdepth 1 -type f -exec du -b --summarize '{}' \; | cut -f1 ); + + echo "$bytes" && return 0; +}; # return size of dir immediate contents +main() { + #size_large="100000000"; # 100 MB + + #yell "DEBUG:arg1:$1"; # debug + #yell "DEBUG:arg2:$2"; # debug + + # check dependencies + if ! check_depends; then die "FATAL:Missing dependencies."; fi; + + # check args + if ! checkInt "$1"; then + die "FATAL:Not an int:$1"; + else + size_l_mb="$1" + size_l_b="$(( size_l_mb * 1000000 ))"; + fi; + if [[ ! -d "$2" ]]; then + die "FATAL:Not a dir:$2"; + else + dir_in="$2"; + fi; + + # check each dir size contents + while IFS= read -r -d $'\0' pathdir; do + #yell "DEBUG:Checking:pathdir:$pathdir"; # debug + if [[ ! -d "$pathdir" ]]; then continue; fi; + dir_size="$(size_dir_contents "$pathdir";)" + #yell "DEBUG:dir_size:$dir_size"; # debug + if [[ $dir_size -gt "$size_l_b" ]]; then + printf "%s\n" "$pathdir"; # output + fi; + #sleep 1; # debug + done < <(find "$dir_in" -type d -print0); + + return 0; +}; + +main "$@";