Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/lib/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Functions for statistics and analysis.

## anal-disk-usage

Analyze disk usage. Prints the total disk space used by each sub-directory in any directory in ascending order.
Analyze disk usage. Prints the disk space used by each sub-directory in any directory in ascending order.
Also reports how many percent each sub-directory's size takes up in total size.

**Usage**

Expand Down
12 changes: 9 additions & 3 deletions lib/analytics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
## @param $1 Dir to check
## @default Current working dir
####################################
# shellcheck disable=SC2312
anal-disk-usage() {
local dir="${1:-.}"
shift
# shellcheck disable=SC2312
du "${dir}" -hd1 "${@}" 2>/dev/null | sort -h
local total size dir

total=$(du --summarize --block-size=1 "${dir}" | cut -f1)

while IFS= read -r -d '' line; do
read -r size dir <<<"${line}"
printf "%3d%% %8s %s\n" "$((size * 100 / total))" "$(numfmt --to=iec "${size}")" "${dir}"
done < <(du --null --block-size=1 -d1 "${dir}" 2>/dev/null | sort -nz)
}