#! /usr/bin/gawk -f # Reads the output of {find-files-size-date}, sorted by file number. Prints a sumary with number of files, total bytes, and max bytes by directory and subdirectory. BEGIN { abort = -1; init() } (abort >= 0) { exit(abort) } // { sz=$1; dt=$2; fn=$3 gsub(/^[.\/]*[\/]/, "", fn) dp1 = split(fn, dir, "/") # printf " dp1 = %d\n", dp1 > "/dev/stderr" if (dp1 == 0) { prog_error("blank fn?") } dp = dp1-1 # Count of directory parts in this file. # Find the num of {dpeq} of directory parts that match the current saved ones: dpeq = dpo; k = dpo while (k > 0) { if (dir[k] != diro[k]) { dpeq = k-1 } k--; } # Dump the saved data for subdirectories that don't match this one: dumpit(dpeq); # Clear counts for new directories: clearit(dp,dir) if (dpo != dp) { prog_error(("mismatch dpo = " dpo " dp = " dp)) } if ((dp >0) && (diro[dp] != dir[dp])) { prog_error(("mismatch diro[dp] = " diro[dp] " dir[dp] = " dir[dp])) } # Accumulate this file: nf[dp]++; tsz[dp] += sz if (sz > msz[dp]) { msz[dp] = sz; msu[dp] = dir[dp+1] } } END { if (abort >= 0) { exit(abort) } dumpit(-1) } function init() { # The saved information is for directory with parts {diro[1..dpo]}. # The entries with index 0 are for the grand total. dpo = 0 split("", diro); diro[dpo] = "TOTAL" split("", nf); nf[dpo] = 0 split("", tsz); tsz[dpo] = 0 split("", msz); msz[dpo] = -1 split("", msu); msu[dpo] = "NONE" } function dumpit(dpeq) { # Called when only the first {dpeq} parts of current file matches the stored one. while (dpo > dpeq) { printf "%10d %14d %14d ", nf[dpo], tsz[dpo], msz[dpo] for (k = 1; k <= dpo; k++) { printf "%s/", diro[k] } printf " %s\n", msu[dpo] nf[dpo-1] += nf[dpo] tsz[dpo-1] += tsz[dpo] if (tsz[dpo] > msz[dpo-1]) { msz[dpo-1] = msz[dpo]; msu[dpo-1] = diro[dpo]; } dpo-- } } function clearit(dp,dir) { # Called when {dpo < dp}. Initializes counts for levels {dpo+1..dp}. while (dpo < dp) { dpo++ nf[dpo] = 0 diro[dpo] = dir[dpo] tsz[dpo] = 0 msz[dpo] = -1 msu[dpo] = "NONE" } } function prog_error(msg) { printf "%s:%d: ** program error: %s\n", FILENAME, FNR, msg > "/dev/stderr" printf " [%s]\n", $0 abort = 1; exit(abort) }