2 # Desc: Lists directories immediately containing large amounts of data
3 # Usage: script.sh [int megabytes] [dir]
4 # Example: A directory named `vacation_photos` located somewhere in $HOME/
5 # containing more than 100MB of files (without checking subdirectories of
6 # `vacation_photos`) could be found by running:
7 # $ script.sh 100 "$HOME"
8 # /home/johndoe/Pictures/unsorted/notjunk/vacation_photos
11 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
12 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
13 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
15 # Desc: Checks if arg is integer
18 # Output: - return code 0 (if arg is integer)
19 # - return code 1 (if arg is not integer)
20 # Example: if ! checkInt $arg; then echo "not int"; fi;
25 if [[ $# -ne 1 ]]; then
26 die
"ERROR:Invalid number of arguments:$#";
29 RETEST1
='^[0-9]+$'; # Regular Expression to test
30 if [[ ! $1 =~
$RETEST1 ]] ; then
36 #===Determine function return code===
37 if [ "$returnState" = "true" ]; then
42 } # Checks if arg is integer
46 if ! command -v du
1>/dev
/random
2>&1; then flag_return
=1; fi;
48 return "$flag_return";
49 }; # returns 1 if missing dependencies
51 # usage: size_dir_contents [path]
58 #yell "DEBUG:Checking dir:$1";
59 while read -r addend
; do
60 #yell "DEBUG:addend:$addend";
61 if [[ $addend =~
$re ]]; then
62 bytes
="$((bytes + addend))";
64 done < <( find "$1" -maxdepth 1 -type f
-exec du
-b --summarize '{}' \
; | cut
-f1 );
66 echo "$bytes" && return 0;
67 }; # return size of dir immediate contents
69 #size_large="100000000"; # 100 MB
71 #yell "DEBUG:arg1:$1"; # debug
72 #yell "DEBUG:arg2:$2"; # debug
75 if ! check_depends
; then die
"FATAL:Missing dependencies."; fi;
78 if ! checkInt
"$1"; then
79 die
"FATAL:Not an int:$1";
82 size_l_b
="$(( size_l_mb * 1000000 ))";
84 if [[ ! -d "$2" ]]; then
85 die
"FATAL:Not a dir:$2";
90 # check each dir size contents
91 while IFS
= read -r -d $
'\0' pathdir
; do
92 #yell "DEBUG:Checking:pathdir:$pathdir"; # debug
93 if [[ ! -d "$pathdir" ]]; then continue; fi;
94 dir_size
="$(size_dir_contents "$pathdir";)"
95 #yell "DEBUG:dir_size:$dir_size"; # debug
96 if [[ $dir_size -gt "$size_l_b" ]]; then
97 printf "%s\n" "$pathdir"; # output
100 done < <(find "$dir_in" -type d
-print0);